473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
0 1198

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Opinder | last post by:
Hi, For the Python experts out there: Are there any side effects in assigning new variables to the __builtins__ module for the purpose of exposing variables to imported modules. For...
0
by: Mathieu Fenniak | last post by:
Good day, I'm writing an application which creates modules and runs Python code on the fly, embedding Python. The main function of the code is loaded into the __main__ module: (Error checking...
1
by: Matteo Merli | last post by:
Hi, is there a way to save objects in the __builtins__ namespace? the goal is to make them available in all the modules of my app, without reimporting all the things and keep references... The...
1
by: Michael Hohn | last post by:
Hi, using the file builtin_check.py with content # Module builtin_check # Inconstency in the binding of __builtins__ def get_binding(name): return locals() def get_global_binding(name):
4
by: Collin Winter | last post by:
Hallo all, As it currently stands, the type of the global __builtins__ differs depending on whether you're in the __main__ namespace (__builtins__ is a module) or not (its a dict). I was...
1
by: Adam Hupp | last post by:
I've noticed some unexpected behavior with __builtins__ during module import. It seems that during module import __builtins__ is a dict but at all other times it is a module. For example, if...
3
by: loquehumaine | last post by:
Hi there, I'm a newby in python (I know a little in programmation) and I have a lot of questions on builtins but my first one is about modules... I have seen that if I type help() at a prompt,...
0
by: Patrick Maupin | last post by:
__builtins__ in 2.5.2 doesn't seem to behave like I remember it did the last time I did some custom stuff with it, a very long time ago. This isn't surprising, because of ongoing optimization,...
3
by: Gabriel Genellina | last post by:
En Sun, 07 Sep 2008 14:00:48 -0300, Patrick Maupin <pmaupin@gmail.comescribió: Python takes some shortcuts when dealing with builtins. I'll just describe what happens (I won't say whether it is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.