truly global variables

Peter Hansen peter at engcorp.com
Thu Feb 19 16:31:44 CET 2004


ketulp_baroda at yahoo.com wrote:
> 
> Global variables are not truly global in Python ; they are only global
> within a module namespace. I want a variable such that if i change its
> value in any module then it should be reflected in other modules too.
> What I mean is I wanta varibale say i whose initial value is 1.
> If I change value of i in foo.py to say 5 and now I print value of i
> in bar.py ,it should print 5.How to do this??

Provided you're willing to have a dot in the name of the variable,
this is actually quite possible, as Ben shows even though he markets
his approach poorly by saying what you want is not possible. :-)

If you can explain why you wouldn't want to just use a module to
contain your globals, we'll be able to suggest alternatives (and
there are some alternatives, they're just not usually as clean or
simple).

-Peter



More information about the Python-list mailing list