FAQ
How to set local variables based on dictionary contents?

Search Discussions

  • Peter Otten at Jan 3, 2010 at 9:57 pm

    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(aB, 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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 3, '10 at 9:09p
activeJan 3, '10 at 9:57p
posts2
users2
websitepython.org

2 users in discussion

Joan Miller: 1 post Peter Otten: 1 post

People

Translate

site design / logo © 2022 Grokbase