problem with global variable in a module

hollowspook hollowspook at gmail.com
Sat Nov 25 09:09:55 EST 2006


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




More information about the Python-list mailing list