Eric V. Smith added the comment:
Thanks for the great explanation, Steven. And I agree with Josh that changing
the exception text would lead to blindly adding nonlocal or global in a
superficial attempt to get the code to work. The much more likely problem is
already mentioned: reference bef
Josh Rosenberg added the comment:
I'm inclined to close as Not a Bug as well. I'm worried the expanded error
message would confuse people when they simply failed to assign a variable, and
make them try bad workarounds like adding global/nonlocal when it's not the
problem, e.g.:
def foo(bar)
Steven D'Aprano added the comment:
To clarify further, unlike (say) Lua, Python doesn't allow variables to change
scope part-way through a function. (At least not without hacking the
byte-code.) In any function, a name refers to precisely one of (1) a local, (2)
a nonlocal, and (3) a global,
Steven D'Aprano added the comment:
The behaviour and error message is correct, and your interpretation is
incorrect. You are not assigning to a closure variable on line 4; you are
printing an unbound local variable on line 3, precisely as the error message
says. That may not match your *inte
New submission from kolia :
def outer(a):
def inner():
print(a)
a = 43
return inner
t = outer(42)
print(t())
Outputs:
~/Documents/repro.py in inner()
1 def outer(a):