accessing module global vars by name

Discussion in 'Python' started by Martin Drautzburg, Dec 20, 2004.

  1. Withing a module I can assign a value to a global var by assigning to
    it in the outermost scope. Fine.

    But how can I do this if the attribute name itself is kept in a
    variable. Once the module is loaded I can access the module's
    namespace no problem, but inside the module the dictionary is not yet
    present right ?

    IOW how can I write something like

    # xxx.py

    for varName in ("foo", "bar"):
    magic.varName = 1

    so I can later refer to them as

    # yyy.py
    import xxx
    x = xxx.foo
    y = xxx.bar
    Martin Drautzburg, Dec 20, 2004
    #1
    1. Advertising

  2. Martin Drautzburg wrote:
    > IOW how can I write something like
    >
    > # xxx.py
    >
    > for varName in ("foo", "bar"):
    > magic.varName = 1
    >


    I think you want to use the dict returned by globals(). Modifying this
    dict can add/remove names from the global scope.[1]

    >>> foo

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    NameError: name 'foo' is not defined
    >>> bar

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    NameError: name 'bar' is not defined
    >>> for var_name in ['foo', 'bar']:

    .... globals()[var_name] = True
    ....
    >>> foo

    True
    >>> bar

    True
    >>> del globals()['foo']
    >>> foo

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    NameError: name 'foo' is not defined

    Steve

    [1] As an aside, be careful not to try the same thing with locals().
    locals() returns a dict that won't modify names in the local scope.
    Steven Bethard, Dec 20, 2004
    #2
    1. Advertising

  3. Martin Drautzburg

    Peter Hansen Guest

    Martin Drautzburg wrote:
    > Withing a module I can assign a value to a global var by assigning to
    > it in the outermost scope. Fine.
    >
    > But how can I do this if the attribute name itself is kept in a
    > variable. Once the module is loaded I can access the module's
    > namespace no problem, but inside the module the dictionary is not yet
    > present right ?


    Look into the builtin function globals()...

    -Peter
    Peter Hansen, Dec 20, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    392
  2. Kepes Krisztian

    How to access my module global vars ?

    Kepes Krisztian, Nov 25, 2003, in forum: Python
    Replies:
    0
    Views:
    273
    Kepes Krisztian
    Nov 25, 2003
  3. Fernando Rodríguez

    Trouble accessing global vars

    Fernando Rodríguez, Sep 4, 2004, in forum: Python
    Replies:
    6
    Views:
    450
    Alex Martelli
    Sep 6, 2004
  4. Linuxguy123
    Replies:
    7
    Views:
    687
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    393
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page