exec code in "globaldict"

  • Thread starter Gerald Senarclens de Grancy
  • Start date
G

Gerald Senarclens de Grancy

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.
Thanks for any kind of help.
cheers, Gerald
 
J

Josiah Carlson

Gerald Senarclens de Grancy said:
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
 
T

Terry Reedy

Gerald Senarclens de Grancy said:
Hi!
I kept searching my online and offline python ressources to figure out
how to execute some code in the global dictionary.

Perhaps you are thinking of builtins. If so, leave it alone.
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.

Each module has its own global dict. For what you want, define a module
globals.py or options.py or config.py or whatever, have that module read
the options, and then import that module into all other modules.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,210
Messages
2,571,091
Members
47,691
Latest member
Jenny-jane

Latest Threads

Top