Global variables and modules

John Machin sjmachin at lexicon.net
Fri Dec 24 16:39:41 EST 2004


dkeeney at travelbyroad.net wrote:
> I have a newby type question on how global variables work between
> modules.
>
> I have a module test2.py that defines a global variable as well as
two
> functions to operate on that variable.  A script test1.py imports the
> three names and prints out values of the global between operations,
and
> the results aren't what I expected.
[snip]
> I am looking for a way to share data between modules.  Can
> you advise me on my error?

Yes. Your error is that you are looking at global variables as "a way
to share data between modules". The advice is "Don't do that". Limited
use of global variables whose values are updated is barely tolerable
within a single module that will be executed by a single thread of a
single process. Otherwise you can end up with monstrosities like
database "forms" applications with hundreds of globals. Google for
concepts like "modularity" and "information hiding". Then read about
OOP.




More information about the Python-list mailing list