Oddity in 2.4 with eval('None')

Steve Holden steve at holdenweb.com
Mon Dec 20 15:17:12 EST 2004


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__.None
None
  >>> print eval("__builtins__.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



More information about the Python-list mailing list