Is it safe to modify the dict returned by vars() or locals()

Discussion in 'Python' started by Helmut Jarausch, Dec 1, 2008.

  1. Hi,

    I am looking for an elegant way to solve the following problem:

    Within a function

    def Foo(**parms)

    I have a list of names, say VList=['A','B','C1']
    and I like to generate abbreviation
    _A identical to parms['A']

    for that I could write

    def Foo(**parms) :
    for N in VList :
    if N in parms :
    vars()[N]= parms[N]
    else :
    vars()[N]= None

    Does this work, is it typical Python?

    Many thanks for a hint,
    Helmut.

    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Dec 1, 2008
    #1
    1. Advertisements

  2. Helmut Jarausch

    Chris Rebert Guest

    Could you explain what you mean by that? Your sample code doesn't seem
    to do any "abbreviation"...
    Otherwise I don't see why you don't just have a proper parameter list.
    locals()
    Update and return a dictionary representing the current local symbol table.
    *Warning*:
    The contents of this dictionary should not be modified; changes
    may not affect the values of local variables used by the interpreter.
    Free variables are returned by locals() when it is called in a
    function block. Modifications of free variables may not affect the
    values used by the interpreter. Free variables are not returned in
    class blocks.

    As the warning states, it modifying the dict doesn't really work
    (except at the module level, but that's an implementation detail IIRC)
    For example:.... a = 3
    .... l = locals()
    .... l['a'] = 5
    .... print a
    ....3

    In any case, it'd be considered a bit of a hack.

    Cheers,
    Chris
     
    Chris Rebert, Dec 1, 2008
    #2
    1. Advertisements

  3. Helmut Jarausch

    Peter Otten Guest

    locals() gives you a copy of the local namespace. No changes to the copy are
    written back to that namespace.

    In idiomatic python you'd just use the dictionary. If you are bothered with
    the non-existent keys, make a copy
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 'x'

    or, setting any non-existent key to None:
    (1, None, 3)

    If you insist on manipulating the namespace you can use exec:
    .... exec "\n".join("%s = parms.get(%r)" % (n, n) for n in
    ["a", "b", "c"])
    .... return a, b, c
    ....(None, 20, None)

    Peter
     
    Peter Otten, Dec 1, 2008
    #3
  4. In my application parms contains field names of an html form iff these fields
    have been modified.
    I'd like to use the short name _A instead of the longer expression parms['A']



    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Dec 2, 2008
    #4
  5. In my application parms contains field names of an html form iff these fields
    have been modified.
    I'd like to use the short name _A instead of the longer expression parms['A']



    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Dec 2, 2008
    #5
  6. Many thanks,
    that's a very elegant solution.

    Helmut.



    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Dec 3, 2008
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.