如果运行此脚本:
def main_function():
b = 'b'
def nested_function():
print(b)
#b = 'value change'
nested_function()
main_function()
一切正常.
如果我运行另一个脚本:
def main_function():
b = 'b'
def nested_function():
#print(b)
b = 'value change'
nested_function()
main_function()
一切都很好.如您所见,这里我更改了nested\u函数()中的内部行.
但如果我用这两行
def main_function():
b = 'b'
def nested_function():
print(b)
b = 'value change'
nested_function()
main_function()
然后,出现了一个错误.
UnboundLocalError:赋值前引用了局部变量"b"
我不明白为什么会这样.