"Sean Givan" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi.  I'm new to Python, and downloaded a Windows copy a little while
> ago.  I was doing some experiments with nested functions, and ran into
> something strange.
Experiments are good.  Strange can be instructive.
...
> I'm thinking this is some bug
Blaming the interpreter is not so good, but amazingly common among 
newcomers ;-)

> where the interpreter is getting ahead of  itself,
...
>  Or am I doing something wrong?

In a sense, you got ahead of yourself.  And the issue has nothing to do 
with nested scopes per se.  When things seem strange, try a simpler 
experiment.
>>> x=1
>>> def f():
 print x
 x = 2

>>> f()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in -toplevel-
    f()
  File "<pyshell#4>", line 2, in f
    print x
UnboundLocalError: local variable 'x' referenced before assignment

The compiler compiles functions in two passes: the first to classify names 
as local or global (or nested if relevant, but not really so here), the 
second to generate bytecodes which depend on that classification.

Terry Jan Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to