Recover handle to shadowed builtin?

Roy Smith roy at panix.com
Wed Jan 8 15:15:25 EST 2014


In article <mailman.5194.1389210743.18130.python-list at python.org>,
Roy Smith  <roy at panix.com> wrote:
>I'm working with ipython's pylab mode, which replaces the builtin sum() =
>with the one from numpy:
>[...]
>Is there any way to recover a reference to the builtin sum()?

Sigh.  I figured this out myself.  What you want is __builtins__.sum ...

BUT, not only does pylab overwrite sum(), it overwrites __builtins__
as well!  Instead of a module, it's now a dict.  You can still get at
the builtin sum, but you need to do __builtins__["sum"]

========================================================   
$ ipython
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
Type "copyright", "credits" or "license" for more information.

IPython 0.12.1 -- An enhanced Interactive Python.
[...]
In [1]: type(__builtins__)
Out[1]: module
========================================================   
$ ipython --pylab
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
Type "copyright", "credits" or "license" for more information.

IPython 0.12.1 -- An enhanced Interactive Python.
[...]
In [1]: type(__builtins__)
Out[1]: dict
========================================================   

I am slowly coming to the conclusion that the best way to deal with
pylab is to not use it.  Overwriting sum() with a different sum() I
can accept, as a useful shortcut.  Overwriting __builtins__ seems like
wanton vandalism of the namespace.



More information about the Python-list mailing list