variable scoping problem

Kristofer Wouk kristofer at hotpop.com
Fri Apr 25 07:04:57 CEST 2003


Tung Wai Yip wrote:
> 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
> 
Sorry, missed the bit about the error. I messed up...





More information about the Python-list mailing list