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

Helmut Jarausch jarausch at igpm.rwth-aachen.de
Wed Dec 3 11:50:48 CET 2008


Duncan Booth wrote:
> Helmut Jarausch <jarausch at igpm.rwth-aachen.de> wrote:
> 
>> Chris Rebert wrote:
>>> On Mon, Dec 1, 2008 at 1:01 PM, Helmut Jarausch <jarausch at skynet.be>
>>> wrote: 
>>>> 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']
>>> 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. 
>> 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'] 
>>
>>
> You haven't yet explained why you don't just do:
> 
> def foo(A=None, **parms):
>    ...
> 
> The code to call foo remains the same as before, but Python will unpack 
> the named parameter into the local variable A for you automatically. Any 
> other parms that you didn't know about in advance remain in the separate 
> dictionary.
> 

Many thanks,
that's a very elegant solution.

Helmut.



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany



More information about the Python-list mailing list