Don't understand global variables between modules

Fredrik Lundh fredrik at pythonware.com
Mon Feb 21 05:48:38 EST 2005


Bart wrote:

> 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?

because running a script isn't the same thing as importing it.  try adding
"print __name__" lines before your other print statements so you can see
who's printing what.

> Is there more than one global space?

in this case, there are more module namespaces than you think.

this page might help (especially the "Using Modules as Scripts" section):

    http://effbot.org/zone/import-confusion.htm

</F> 






More information about the Python-list mailing list