exec code in "globaldict"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerald Senarclens de Grancy

    #1

    exec code in "globaldict"

    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
  • Josiah Carlson

    #2
    Re: exec code in "globaldic t"


    Gerald Senarclens de Grancy <santa@sbox.tug raz.at> wrote:[color=blue]
    >
    > 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.[/color]

    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.pre f1
    ...

    - Josiah

    Comment

    • Terry Reedy

      #3
      Re: exec code in &quot;globaldic t&quot;


      "Gerald Senarclens de Grancy" <santa@sbox.tug raz.at> wrote in message
      news:Nizid.5589 6$z77.3059@news .chello.at...[color=blue]
      > Hi!
      > I kept searching my online and offline python ressources to figure out
      > how to execute some code in the global dictionary.[/color]

      Perhaps you are thinking of builtins. If so, leave it alone.
      [color=blue]
      > 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.[/color]

      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



      Comment

      Working...