variable scoping problem

Tung Wai Yip tungwaiyip at yahoo.com
Thu Apr 24 20:30:30 EDT 2003


Consider this

>>> x=1
>>> def foo():
...   print x
...
>>> def bar():
...   #print x   <- referenced before assignment error
...   x=2
...   print x
...
>>> foo()
1
>>> bar()
2
>>> print x
1
>>>

What rules in Python explain the scoping of x? Is there a name for the
scope of the first x? 'module' scope? Why is bar() not able to access
x but foo() can?

Wai Yip Tung





More information about the Python-list mailing list