Issue 14891: An error in bindings of closures (original) (raw)
Issue14891
Created on 2012-05-23 16:19 by Frederick.Ross, last changed 2022-04-11 14:57 by admin. This issue is now closed.
Messages (4) | ||
---|---|---|
msg161432 - (view) | Author: Frederick Ross (Frederick.Ross) | Date: 2012-05-23 16:19 |
The following code throws an UnboundLocal error: def f(x): def g(): x = x + "a" return x return g() f("b") | ||
msg161437 - (view) | Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * ![]() |
Date: 2012-05-23 16:34 |
This is expected behavior: http://docs.python.org/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value | ||
msg161442 - (view) | Author: Frederick Ross (Frederick.Ross) | Date: 2012-05-23 17:49 |
Assignment in Python creates a new binding. Whether the new binding shadows or replaces an old binding should be irrelevant. This behavior is inconsistent with that. Please fix expectations, and then Python interpreter. | ||
msg161449 - (view) | Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * ![]() |
Date: 2012-05-23 18:52 |
What fails here is the evaluation of "x", not the assignment! You are right concerning the assignment, the outer definition has no effect at all. The very presence of "x = " in the function code turns x into a local variable for the whole function; so 'x + "a"' fails because the local variable has not value yet. Python, unlike Lisp, defines scopes lexically, not dynamically. There is no "previous" binding, but "local" or "outer" scopes. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:57:30 | admin | set | github: 59096 |
2012-05-23 18:52:29 | amaury.forgeotdarc | set | status: open -> closedresolution: not a bugmessages: + |
2012-05-23 17:49:01 | Frederick.Ross | set | status: closed -> openresolution: not a bug -> (no value)messages: + |
2012-05-23 16:34:51 | amaury.forgeotdarc | set | status: open -> closednosy: + amaury.forgeotdarcmessages: + resolution: not a bug |
2012-05-23 16:19:36 | Frederick.Ross | create |