467,921 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,921 developers. It's quick & easy.

Re: why? __builtins__ key added from eval

On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote:
I want to use eval to evaluate wx.TextCtrl inputs. How can I keep python
from adding the __builtins__ key to mydict when I use it with eval?
Other wise I have to __delitem__('__builtins__') everytime I use eval?
>>>mydict = {'a':2,'b':3}
eval('a*b',mydict)
6
>>>mydict
{'a': 2, '__builtins__': {'IndexError': <type 'exceptions.IndexError'>,
...(I'll spare you the rest)...}, 'b': 3}

Also, how come eval has this behavior? Is it desirable?

-Bill
when you pass mydict, it is used as the global variables in the eval,
right? Then, you passed a code to eval('...', mydict), sometimes you
might create global variable inside eval, and you want to see the value
of that inner global, that's why python modified mydict as a side-effect.

Then what is __builtins__ doing there? Because when eval created an
environment for the code to run, python interpreter need to define things
that it used by itself. The __builtins__ contained all built-in names
that is always available in python (python have to store it somewhere,
the built-in names couldn't be bound by magic, right?).

You could remove the builtin if you don't think it is necessary for you.
Or you could do "dictionary comprehension" that collects only names you
require (actually use generator comprehension then do a dict()
conversion, dictionary comprehension was rejected some time ago)

e.g.:
newdict = dict(k, v for k, v in mydict.iteritems() if k in ['a', 'b'])

or you could make a copy of mydict before passing it to eval.

Last note: eval is evil. (no pun intended)

Oct 2 '08 #1
  • viewed: 1020
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mathieu Fenniak | last post: by
1 post views Thread by Matteo Merli | last post: by
1 post views Thread by Michael Hohn | last post: by
4 posts views Thread by Collin Winter | last post: by
3 posts views Thread by loquehumaine | last post: by
reply views Thread by Patrick Maupin | last post: by
3 posts views Thread by Gabriel Genellina | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.