Nested scopes: why is it weird?

Fredrik Lundh fredrik at
Fri Sep 7 19:23:48 CEST 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.

    "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.


<!-- (the eff-bot guide to) the python standard library:

More information about the Python-list mailing list