My scope is broke

Peter Hansen peter at engcorp.com
Sat Dec 21 02:39:47 EST 2002


Travis Beaty wrote:
> 
> Ah ... yes, I had totally misunderstood what "global" does.   I believe
> I have a better grasp of global now.  One quick question about a piece
> of the code that you supplied, however:
> 
> >s = 0
> >def Aglobal():
> >        global s
> >        s = 132         #this "s" is global from the view point of this
> >                        #function
> >
> Had s not been defined in the outer scope, would the variable s declared
> with global within Aglobal() still be accessible outside the function?
> Or would s now be something like C++'s static int within Aglobal()?

The "global" statement actually affects the code generated
by the compiler.  In this case, having seen the "global s"
statement, the compiler makes darn sure to create s in the
global namespace of the module, *outside* the function, 
even if there was no previous definition like "s = 0".

-Peter



More information about the Python-list mailing list