Don't understand global variables between modules

Bart bvdeenen at access-four-all.nl.invalid
Mon Feb 21 05:38:23 EST 2005


Hi all

I don't understand globals between multiple modules in a python program. I
really don't. I've narrowed it down to the following two very simple
programs a.py and b.py. When I run a.py I get the following output:

inc:  2
A:  2
inc:  3
B:  3
C:  1
I don't understand the last line at all. Why is my x variable 1 after having
been incremented twice? Is there more than one global space? Is this
backreference to the original a.py not allowed?

I could use some help.

Thanks

Bart van Deenen



a.py:
---------------------------------
import b
x=1

def inc():
    global x
    x+=1
    print "inc: ",x

if __name__=="__main__":
    b.test()
    print "C: ",x

b.py:
---------------------------------
def test():
    import a
    a.inc()
    print "A: ",a.x
    a.inc()
    print "B: ",a.x




More information about the Python-list mailing list