473,602 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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',m ydict)
6
>>>mydict
{'a': 2, '__builtins__': {'IndexError': <type 'exceptions.Ind exError'>,
...(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.iteritem s() 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 1211

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

Similar topics

1
1800
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 example.
0
1745
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 omitted for brevity.) PyObject* module = PyImport_AddModule("__main__"); PyObject* moduleDict = PyModule_GetDict(module); PyObject* runRetval = PyRun_String(codeStr, Py_file_input, moduleDict, moduleDict);
1
1777
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 curious thing is that when i try from the interactive interpreter the thing just works: >>> def a(): .... print "ciao"
1
1362
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
1477
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 recently tripped up by this discrepancy, and googling the issue brings up half-a-dozen or so c.l.p threads where others have been bitten by this, too. I'd like to propose that in Py3.0 (if not earlier), __builtins__ will be the same type regardless of...
1
1733
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 the file testmod.py has these contents: print type(__builtins__) print "has str attr", hasattr(__builtins__, 'str') The output differs depending on how it is run:
3
4166
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, and then 'modules', I'll be given a list of all modules available, thanks to this group.. But I have seen the differences between them and the one in dir(__builtins__). Why are some modules in __builtins__ and others don't ? (UserDict for example)
0
1076
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, but it's hard to google for '__builtins__' so I didn't really find any documentation on the current CPython behavior, which in some cases seems quite strange to me. The documentation node at http://docs.python.org/ref/naming.html has a "here be...
3
1365
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 "right" or "wrong"). The exec statement, when given a string source, compiles it and eventually calls PyEval_EvalCodeEx, which creates a new frame using PyFrame_New and finally executes it. A frame object contains a pointer to the previous frame,...
0
8401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.