problem with 'global'

O

oyster

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
 
M

Mel

oyster said:
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.
 
D

Duncan Booth

Mel said:
oyster said:
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."
 
M

Mel

Duncan said:
Mel said:
oyster said:
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.
 
G

Gabriel Genellina

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

Marc 'BlackJack' Rintsch

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
 
D

Duncan Booth

Marc 'BlackJack' Rintsch said:
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.
 
G

Gabriel Genellina

En Mon, 21 Jan 2008 17:36:29 -0200, Duncan Booth
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top