Set variables based on dictionary
Peter Otten
__peter__ at web.de
Sun Jan 3 16:57:09 EST 2010
Joan Miller wrote:
> How to set local variables based on dictionary contents?
>>> def f(**d):
... for k, v in d.iteritems(): exec "%s = v" % k
... return locals()
...
>>> f(a=42, b="yadda")
{'a': 42, 'k': 'b', 'b': 'yadda', 'd': {'a': 42, 'b': 'yadda'}, 'v':
'yadda'}
>>> f(**{"print 'warning: this can execute arbitrary code'; a":42})
warning: this can execute arbitrary code
{'a': 42, 'k': "print 'warning: this can execute arbitrary code'; a", 'd':
{"print 'warning: this can execute arbitrary code'; a": 42}, 'v': 42}
Peter
More information about the Python-list
mailing list