By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

problem with global variable in a module

P: n/a
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

Nov 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"hollowspook" <ho*********@gmail.comwrote:
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'.

Nov 25 '06 #2

P: n/a
hollowspook schrieb:
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-...foo-import.htm

Diez
Nov 25 '06 #3

P: n/a
Thanks for all the replys.
"Diez B. Roggisch д
"
hollowspook schrieb:
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-...foo-import.htm

Diez
Nov 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.