By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

Re: why? __builtins__ key added from eval

P: n/a
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}
{'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?

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)

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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.