problem with global variable in a module

H

hollowspook

def aa():
global b
b=b+1
print b

b=1
aa()

The above code runs well in python shell.

However I have a problem as follows.

new a file named test.py
------------------------------------------------------------------------

def aa():
global b
b=b+1
print b
-------------------------------------------------------------------------

Then in python shell,

from test import *
b=1
aa()

The error message is :
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "test.py", line 3, in aa
b=b+1
NameError: global name 'b' is not defined

Why this happens? Please do me a favor.
Thanks in advance
 
D

Duncan Booth

hollowspook said:
from test import *
b=1
aa()

The error message is :
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "test.py", line 3, in aa
b=b+1
NameError: global name 'b' is not defined

Why this happens? Please do me a favor.

The global statement makes a name global to a module, it doesn't make it
global to the entire program. The function aa is in module test, the
statements you entered in the shell are in a different module called
__main__.

The particular form of import you used may also be confusing you: just
because you imported '*' doesn't change the function: 'aa' was defined in
the module 'test' and that is where it still looks for its global
variables, all the import did was define a new name 'aa' in the __main__
module which refers to the same function object as 'aa' in 'test'.
 
D

Diez B. Roggisch

hollowspook said:
def aa():
global b
b=b+1
print b

b=1
aa()

The above code runs well in python shell.

However I have a problem as follows.

new a file named test.py
------------------------------------------------------------------------

def aa():
global b
b=b+1
print b
-------------------------------------------------------------------------

Then in python shell,

from test import *
b=1
aa()

The error message is :
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "test.py", line 3, in aa
b=b+1
NameError: global name 'b' is not defined

Why this happens? Please do me a favor.

Because python does not know "real" globals - globals are always only
global on a module level. Now using the "from module import *"-syntax
asks for trouble. Because your global b above is global to the
__main__-module, not to the module test. But the aa-function expects it
to live in tests.

To make your example work, do it as this:

import test

test.b = 1
test.aa()


See
http://effbot.org/pyfaq/tutor-whats-the-difference-between-import-foo-and-from-foo-import.htm

Diez
 

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

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top