[issue37568] Misleading UnBoundLocalError on assignment to closure variable

2019-07-12 Thread Eric V. Smith
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

[issue37568] Misleading UnBoundLocalError on assignment to closure variable

2019-07-11 Thread Josh Rosenberg
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

[issue37568] Misleading UnBoundLocalError on assignment to closure variable

2019-07-11 Thread Steven D'Aprano
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

[issue37568] Misleading UnBoundLocalError on assignment to closure variable

2019-07-11 Thread Steven D'Aprano
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

[issue37568] Misleading UnBoundLocalError on assignment to closure variable

2019-07-11 Thread kolia
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):