updating local()

Flavio fccoelho at gmail.com
Thu Oct 6 09:35:03 EDT 2005


I wish all my problems involved just a couple of variables, but
unfortunately the real interesting problems tend to be complex...

As a last resort this problem could be solved by something like this:

def fun(**kw):
    a = 100
    for k,v in kw.items():
        exec('%s = %s'%(k,v))
    print locals()


>>> fun(**{'a':1,'b':2})
{'a': 1, 'k': 'b', 'b': 2, 'kw': {'a': 1, 'b': 2}, 'v': 2}

But that would be utterly stupid! So much for not being able to write
to locals()....

any better Ideas?




More information about the Python-list mailing list