还是一样,看一个例子:
x = 0 def outer(): x = 1 def inner(): global x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x)
结果
# inner: 2 # outer: 1 # global: 2
global 是对整个环境下的变量起作用,而不是对函数类的变量起作用。