Global Variables in Modules

Jeff Clement jsc-news at bluesine.com
Mon Feb 11 13:10:25 EST 2002


Good Afternoon All,

I have a project where it would be extremely handy (albeit maybe not so clean codeing), to have a global variable in one module be available to another module (without a lot of work.  I know I can do module.varname = varname from the importing module but I would prefer not to).

Below is my sample program.  I would like to launch test.py and declare some global variable A, then import test_mod and hopefully get access to A from within test_mod.  

What I'm actually doing is running a code block in an exec block and declaring a bunch of globals for that exec.  The code block imports some modules and I need those to be able to see the global variables.  I can make this work by adding them to __builtins__ but that doesn't seem right somehow :)

Anyone have any ideas?

Thanks,
Jeff

---- test program -----------------------------------------

$ python test.py

test.py
-------
A=100
import test_mod

test_mod.py
-----------
print A



More information about the Python-list mailing list