global variables shared across modules

Patrick Maupin pmaupin at gmail.com
Sat Sep 10 01:37:56 CEST 2005


MackS wrote:
>     print "inside fun(): " + global_var
...
> How can I get the changed value to "persist" in such a way that it
> isn't reset when control leaves fun()? Why is it even reset in the
> first place? After all, the module has already been imported (and the
> initialization of global_var executed) *before* fun() runs, right?
>
> Any clarification and help in solving this would be great

It _does_ persist and it _isn't_ reset.  Try:

import shared
print shared.global_var

at the end of your program, and you will see that you have bound the
identifier global_var in your main program to the same immutable object
that the identifier global_var in shared was bound to, and then you
_changed_ the identifier global_var in shared to point to a different
object.




More information about the Python-list mailing list