Python Global State

MRAB google at mrabarnett.plus.com
Tue Feb 3 17:10:32 CET 2009


er wrote:
> Simple question, I think: Is there a way to make a completely global
> variable across a slew of modules?  If not, what is the canonical
> way to keep a global state?  The purpose of this is to try to prevent
> circular module imports, which just sort of seems nasty.  Thank you!
> 
Simple answer: no.

You can put your global state into a module which is imported wherever 
it's needed:

my_globals.py
-------------
foo = ""


module_1.py
-----------
import my_globals
...
my_globals.foo = "FOO"
...


module_2.py
-----------
import my_globals
...
print my_globals.foo
...




More information about the Python-list mailing list