why is this not working? (nested scope question)

biner.sebastien at ouranos.ca biner.sebastien at ouranos.ca
Wed Jul 26 16:00:20 EDT 2006


I have a problem understanding the scope of variable in nested
function. I think I got it nailed to the following example copied from
Learning Python 2nd edition page 205. Here is the code.

def f1() :
    x=88
    f2()
def f2() :
    print 'x=',x
f1()

that returns an error saying that "NameError: global name 'x' is not
defined". I expected f2 to "see" the value of x defined in f1 since it
is nested at runtime. My reading of the book comforted me in this.

What am I missing? Shouldn't the E of the LEGB rule take care of that.
BTW, I am running this on python 2.3.

Thanks.

Sébastien.




More information about the Python-list mailing list