Nested scopes: why is it weird?
Fredrik Lundh
fredrik at pythonware.com
Fri Sep 7 13:23:48 EDT 2001
Scott Long wrote:
> During pass #1 of compilation, the statement "x = 0" in b() binds x as a
> local variable local to b(). During execution, k = x executes *before* x
> has been assigned, resulting in this flamage.
http://www.python.org/doc/current/ref/execframes.html
"Whether a name is local or global in a code block is determined
by static inspection of the source text for the code block: in the
absence of global statements, a name that is bound anywhere
in the code block is local in the entire code block; all other names
are considered global."
(where "global" should be read as "non-local" in 2.2 and later)
> My first beef here is, why is the exception called UnboundLocalError?
> The variable is certainly bound (into the local scope), it simply has
> not been assigned.
in Python, assignment binds names to objects. "x" hasn't been
bound to any object.
</F>
<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list