Python Global State

Gary Herron gherron at islandtraining.com
Tue Feb 3 17:11:17 CET 2009


er wrote:
> Simple question, I think: Is there a way to make a completely global
> variable across a slew of modules?  

No.

> If not, what is the canonical way to keep a global state?  


But this might satisfy:

Create a module called, perhaps, global.py which contains your variables.

global.py
  VarA = None
  VarB = None

Then any module can import it and read/write the values.

  import global
  global.VarA = ...
  print global.VarB
  # and so forth



> The purpose of this is to try to prevent circular module imports,
> which just sort of seems nasty.  Thank you!

I'm not sure how that will work.


Gary Herron






More information about the Python-list mailing list