Global Variables in Modules

Philip Swartzleonard starx at
Mon Feb 11 22:08:47 EST 2002

Jeff Clement || Mon 11 Feb 2002 10:10:25a:

> 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 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
> -------
> A=100
> import test_mod
> -----------
> print A

Well, i'm doing something like this:
texture_enabled = 1;

if button_pushed = something_or_other:
  toggle( globals.texture_enabled ) # i.e x = not x, a helper

if globals.texture_enabled:

Which is basically the same thing that jason said, except i populate the 
file IN the file. Also, you should probably name it something other than 
'globals', seeing how that is a builtin function name (didn't know at the 
time, got real confused when i went to use it :).

The benefit in either case, is that you put all of your global data into a 
named structure, which makes things less messy. (You could even import it 
as gb or G or something easy to type, just be consistant :)
Philip Sw "Starweaver" [rasx] ::

More information about the Python-list mailing list