test.py
x = 10; # global variable
def func1():
print(x); # prints 10
def func2()
x = x + 1; # IDE shows error: "Unresolved reference of x(RHS of expression)
def func3()
global x;
x = x + 1; # This works
当x具有全局作用域时,为什么func2()不允许我修改它的值,尽管在func1()中可以访问它。以及为什么在func3()的情况下需要显式提及“ global”关键字?
富国沪深
相关分类