Set variables based on dictionary

2010-01-04 Thread Joan Miller
How to set local variables based on dictionary contents?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set variables based on dictionary

2010-01-03 Thread Peter Otten
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
-- 
http://mail.python.org/mailman/listinfo/python-list