oyster wrote:
> why the following 2 prg give different results? a.py is ok, but b.py
> is 'undefiend a'
> I am using Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC
> v.1310 32 bit (Intel)] on win32
> #a.py
> def run():
>         if 1==2:                # note, it always False
>         global a
>         a=1
> 
> run()
> a
> 
> #b.py
> def run():
>         a=1
> 
> run()
> a

The docs seem to be in <http://www.python.org/doc/2.4/ref/global.html> 
but don't look all that helpful.  Probably the key is that global is 
not an executable statement, and isn't affected by being subordinate 
to an if statement.  In a.py the function has been primed to define a 
in the global namespace.  In b.py, not.

Docs do describe global as a "directive to the parser", even though 
it's lumped in with normal statements like print, return, yield, 
raise, etc.

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

Reply via email to