problem with 'global'
Mel
mwilson at the-wire.com
Mon Jan 21 08:44:54 EST 2008
Duncan Booth wrote:
> 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."
I don't think that would stop the OP from thinking the global
statement had to be executed. In the code example, it seems to have
been stuck in a
if 1==2: global a
and it still worked.
Mel.
More information about the Python-list
mailing list