problem with 'global'

Duncan Booth duncan.booth at invalid.invalid
Mon Jan 21 11:36:45 CET 2008


Mel <mwilson at the-wire.com> wrote:

> 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.

Why are you reading Python 2.4 docs? Try 
http://docs.python.org/ref/global.html

The first sentence (which hasn't changed since 2.4) describing the global 
statement seems clear enough to me: "The global statement is a declaration 
which holds for the entire current code block."



More information about the Python-list mailing list