exec code in "globaldict"

Josiah Carlson jcarlson at uci.edu
Thu Nov 4 19:14:22 EST 2004


Gerald Senarclens de Grancy <santa at sbox.tugraz.at> wrote:
> 
> Hi!
> I kept searching my online and offline python ressources to figure out 
> how to execute some code in the global dictionary. Basically I'm reading 
> a options file that defines some variables. I want to access those 
> variables throughout my whole project, so I need them in the global 
> dictionary. Now I think I'm plainly too stupid to figure out what to 
> write in place of the word "globaldict" to have my definitions in this dict.

There is not technically a fully 'global dictionary', but
__builtins__.__dict__ comes close.  Be careful with it.

What is better is to just have a preferences module of some type...

#preferences.py
pref1 = 'hello'
...

#in every other module
import preferences

preferences.pref1
...

 - Josiah




More information about the Python-list mailing list