This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
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) * (Python committer) | 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) * (Python committer) | 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 -> closed resolution: not a bug messages: + msg161449 |
| 2012年05月23日 17:49:01 | Frederick.Ross | set | status: closed -> open resolution: not a bug -> (no value) messages: + msg161442 |
| 2012年05月23日 16:34:51 | amaury.forgeotdarc | set | status: open -> closed nosy: + amaury.forgeotdarc messages: + msg161437 resolution: not a bug |
| 2012年05月23日 16:19:36 | Frederick.Ross | create | |