passing globals to imported module

Mathias Waack M.Waack at gmx.de
Mon Aug 16 15:24:16 EDT 2004


James Tauber wrote:

> 
> Had a question from a colleague that I embarrassingly couldn't
> answer.
> 
> He has a script, foo.py with a global. He wants to import bar.py
> and needs that global available in bar.py

Note that "global" in python means global in the current module. 

> The following obviously doesn't work:
> 
> # foo.py
> my_global = "hello"
> print globals().keys()
> import bar
> 
> 
> # bar.py
> print globals().keys()

If you need the global _after_ the import you can just add in foo.py: 

  bar.my_global = my_global

Another solution would be to use a better design;)

Mathias



More information about the Python-list mailing list