In Python 2.4, although None can't be directly assigned to,
globals()['None'] can still be; however, that won't change the value of
the expression "None" in ordinary statements. Except with the eval
function, it seems:
Python 2.4 (#2, Dec 3 2004, 17:59:05)
[GCC 3.3.5 (Debian 1:3.3.5-2)] on linux2
Type "help", "copyright" , "credits" or "license" for more information. print None
None print eval('None')
None globals()['None'] = "spam" print None
None print eval('None')
spam
I don't really mind this weird behavior; I'm just curious about it. Does
anyone know what might be going on in Python's internals to cause the
difference between "print None" and "print eval('None')"? 6 1482
Leif K-Brooks wrote: In Python 2.4, although None can't be directly assigned to, globals()['None'] can still be; however, that won't change the value of the expression "None" in ordinary statements. Except with the eval function, it seems:
Python 2.4 (#2, Dec 3 2004, 17:59:05) [GCC 3.3.5 (Debian 1:3.3.5-2)] on linux2 Type "help", "copyright" , "credits" or "license" for more information. >>> print None None >>> print eval('None') None >>> globals()['None'] = "spam" >>> print None None >>> print eval('None') spam
I don't really mind this weird behavior; I'm just curious about it. Does anyone know what might be going on in Python's internals to cause the difference between "print None" and "print eval('None')"?
Yes. "print eval('None')" is printing the value of None as defined in
your module's global namespace:
Python 2.4 (#1, Dec 4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright" , "credits" or "license" for more information. import sys globals()['None'] = "FooBar" print sys.modules["__main__"].None
FooBar print __builtins__.No ne
None print eval("__builtin s__.None")
None >>>
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Python 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information. dir()
['__builtins__', '__doc__', '__name__', 'shell'] globals()['None']
Traceback (most recent call last):
File "<input>", line 1, in ?
KeyError: None print None
None print eval('None')
None globals()['None']='wassabi' print None
wassabi print eval('None')
wassabi dir()
['None', '__builtins__', '__doc__', '__name__', 'shell']
I think you are seeing two things and I admit I was confused a bit too.
None is becoming a constant and this is the first step. That is why you
see diffrent results than mine. Also notyice that None didn't exist in
your lnamespace till you defined it then you see all your trouble
start.Shadowing builtins and semi-constants are a bad thing
M.E.Farmer
Steve Holden wrote: Yes. "print eval('None')" is printing the value of None as defined in your module's global namespace:
Right, but why? The expression "None" doesn't worry about the global
namespace when used in normal code; why does it when used in eval()ed code?
Leif K-Brooks wrote: Steve Holden wrote:
Yes. "print eval('None')" is printing the value of None as defined in your module's global namespace:
Right, but why? The expression "None" doesn't worry about the global namespace when used in normal code; why does it when used in eval()ed code?
I have no idea why. Given that eval("globals()['__builtins__'].globals().keys ()")
['None', '__builtins__', '__file__', 'sys', '__name__', '__doc__']
it's beginning to smell a bit like a buglet.
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Leif K-Brooks wrote Yes. "print eval('None')" is printing the value of None as defined in your module's global namespace:
Right, but why? The expression "None" doesn't worry about the global namespace when used in normal code; why does it when used in eval()ed code?
from what I can tell, the mapping of the None symbol to a constant is done
in the peephole optimizer, which doesn't seem to be used when compiling
expressions.
in 2.4: dis.dis(compile ("None", "", "exec"))
1 0 LOAD_CONST 0 (None)
3 POP_TOP
... dis.dis(compile ("None", "", "eval"))
0 0 LOAD_NAME 0 (None)
3 RETURN_VALUE
in 2.3:
dis.dis(compile ("None", "", "exec"))
1 0 LOAD_NAME 0 (None)
3 POP_TOP
... dis.dis(compile ("None", "", "eval"))
0 0 LOAD_NAME 0 (None)
3 RETURN_VALUE
</F>
"Leif K-Brooks" <eu*****@ecritt ers.biz> In Python 2.4, although None can't be directly assigned to, globals()['None'] can still be; however, that won't change the value of the expression "None" in ordinary statements. Except with the eval function, it seems:
Python 2.4 (#2, Dec 3 2004, 17:59:05) [GCC 3.3.5 (Debian 1:3.3.5-2)] on linux2 Type "help", "copyright" , "credits" or "license" for more information. >>> print None None >>> print eval('None') None >>> globals()['None'] = "spam" >>> print None None >>> print eval('None')
spam
I don't really mind this weird behavior; I'm just curious about it. Does anyone know what might be going on in Python's internals to cause the difference between "print None" and "print eval('None')"?
It is a nuance of how None is being made constant.
For backwards compatability, None still has to be in the globals dictionary.
Like all entries in the globals dictionary, you can change it if you try hard
enough (which you did).
For Py2.4, whenever bytecode is generated for a code object, references to
"None" in the globals dictionary are bypassed and replaced with a constant
reference to Py_None, the one, true, singleton instance of None. That will
occur even if you've mucked with None entry in the globals dictionary.
Bytecode for eval() doesn't go through the bytecode optimizer so its dictionary
lookup is retained (producing the effect in your example).
To have made None a literal constant would have been a much more radical step.
Py2.4 simulates this and gives you the real benefit being sought, faster
function execution, without having incurred the costs of having a new literal.
It's possible to fool the simulation, but who cares.
Raymond Hettinger This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kevin Smith |
last post by:
I want to evaluate an expression using eval(), but I only want to supply
the variable values "on demand." For example, let's say that I want to
evaluate 'x+y'. Normally, I would create a dictionary containing 'x'
and 'y' with their corresponding values and pass it in as the globals or
locals. However, in my application, I'm getting an arbitrary expression
from the user. I thought that I could just create a dictionary-like
object and...
|
by: Simon |
last post by:
Am using the following code.
<script language="JavaScript1.2">
function setquantity(productindex,productquantity)
{
//create the reference object
irefname_none = eval("document." + productindex + "none");
<snip>
|
by: Jim Washington |
last post by:
I'm still working on yet another parser for JSON (http://json.org). It's
called minjson, and it's tolerant on input, strict on output, and pretty
fast. The only problem is, it uses eval(). It's important to sanitize the
incoming untrusted code before sending it to eval(). Because eval() is
evil http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx
apparently in every language.
A search for potential trouble with eval() in...
|
by: manstey |
last post by:
Hi,
I have a text file called a.txt:
# comments
I read it using this:
|
by: Abel Daniel |
last post by:
Hi!
A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.
I would like make an interpreter which does this, without the last
part: i.e. where the results are returned as objects, instead of as
strings. I.e. have I would like to see something that behaves like
this:
| |
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= |
last post by:
Hi,
A while ago I asked a question on the list about a simple eval
function, capable of eval'ing simple python constructs (tuples, dicts,
lists, strings, numbers etc) in a secure manner:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/58a01273441d445f/
also pointed to a simple eval function by Fredrik Lundh:
http://effbot.org/zone/simple-iterator-parser.htm. His solution, using
module tokenize, was short and...
|
by: Gordon |
last post by:
I have a script that creates new objects based on the value of a form
field. Basically, the code looks like this.
eval ('new ' + objType.value + '(val1, val2, val3');
objType is a select with the different types of objects you can create
as values.
I really don't like using eval, and it's causing problems, like if I
do something like the following:
|
by: Fett |
last post by:
I am creating a program that requires some data that must be kept up
to date. What I plan is to put this data up on a web-site then have
the program periodically pull the data off the web-site.
My problem is that when I pull the data (currently stored as a
dictionary on the site) off the site, it is a string, I can use eval()
to make that string into a dictionary, and everything is great.
However, this means that I am using eval() on...
|
by: Jean-Paul Calderone |
last post by:
On Thu, 28 Aug 2008 14:51:57 -0700 (PDT), Fett <fettmanchu@gmail.comwrote:
eval and exec are the same. Don't use either with strings from a web page.
Try using a simple format for you data, such as CSV.
Jean-Paul
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |