我有这样的代码(简化版) :
def outer():
ctr = 0
def inner():
ctr += 1
inner()
但 ctr
会导致一个错误:
Traceback (most recent call last):
File "foo.py", line 9, in <module>
outer()
File "foo.py", line 7, in outer
inner()
File "foo.py", line 5, in inner
ctr += 1
UnboundLocalError: local variable 'ctr' referenced before assignment
我该怎么补救?我以为嵌套显微镜可以让我这么做。我试过“全球化”,但还是不行。