[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

problem with 'global'

oyster

1/21/2008 3:22:00 AM

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

Mel

1/21/2008 4:10:00 AM

0

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

Duncan Booth

1/21/2008 10:37:00 AM

0

Mel <mwilson@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/globa...
> but don't look all that helpful.

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

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

Mel

1/21/2008 1:45:00 PM

0

Duncan Booth wrote:
> Mel <mwilson@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/globa...
>> but don't look all that helpful.
>
> Why are you reading Python 2.4 docs? Try
> http://docs.python.org/ref/g...
>
> 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.

Gabriel Genellina

1/21/2008 7:09:00 PM

0

En Mon, 21 Jan 2008 11:44:54 -0200, Mel <mwilson@the-wire.com> escribi�:

> Duncan Booth wrote:
>>
>> 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.

The future statement is another example, even worse:

if 0:
from __future__ import with_statement

with open("xxx") as f:
print f

All import statements are executable, only this special form is not. That
"global" and "__future__" are directives and not executable statements, is
confusing.

--
Gabriel Genellina

Marc 'BlackJack' Rintsch

1/21/2008 7:18:00 PM

0

On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote:

> The future statement is another example, even worse:
>
> if 0:
> from __future__ import with_statement
>
> with open("xxx") as f:
> print f

In Python >=2.5 it's a compile time error if that import is not the very
first statement in a source file.

Ciao,
Marc 'BlackJack' Rintsch

Duncan Booth

1/21/2008 7:36:00 PM

0

Marc 'BlackJack' Rintsch <bj_666@gmx.net> wrote:

> On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote:
>
>> The future statement is another example, even worse:
>>
>> if 0:
>> from __future__ import with_statement
>>
>> with open("xxx") as f:
>> print f
>
> In Python >=2.5 it's a compile time error if that import is not the very
> first statement in a source file.
>
That doesn't appear to be the case. With Python 2.5.1 the example
Gabriel quoted will compile and run.


Gabriel Genellina

1/21/2008 8:36:00 PM

0

En Mon, 21 Jan 2008 17:36:29 -0200, Duncan Booth
<duncan.booth@invalid.invalid> escribi�:

> Marc 'BlackJack' Rintsch <bj_666@gmx.net> wrote:
>
>> On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote:
>>
>>> The future statement is another example, even worse:
>>>
>>> if 0:
>>> from __future__ import with_statement
>>>
>>> with open("xxx") as f:
>>> print f
>>
>> In Python >=2.5 it's a compile time error if that import is not the very
>> first statement in a source file.
>>
> That doesn't appear to be the case. With Python 2.5.1 the example
> Gabriel quoted will compile and run.

Yes, but now I've noticed that the 0 has some magic. The code above works
with 0, 0.0, 0j and ''. Using None, False, () or [] as the condition, will
trigger the (expected) syntax error.

Mmm, it may be the peephole optimizer, eliminating the dead branch. This
function has an empty body:

def f():
if 0: print "0"
if 0.0: print "0.0"
if 0j: print "0j"
if '': print "empty"

py> dis.dis(f)
5 0 LOAD_CONST 0 (None)
3 RETURN_VALUE

The other false values tested (False, None, () and []) aren't optimized
out.

--
Gabriel Genellina