Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recover handle to shadowed builtin?

37 views
Skip to first unread message

Roy Smith

unread,
Jan 8, 2014, 2:52:10 PM1/8/14
to python-list@python.org List
I'm working with ipython's pylab mode, which replaces the builtin sum() with the one from numpy:

In [105]:
sum

Out[105]:
<function numpy.core.fromnumeric.sum>

Is there any way to recover a reference to the builtin sum()?


---
Roy Smith
r...@panix.com



Mark Lawrence

unread,
Jan 8, 2014, 3:11:52 PM1/8/14
to pytho...@python.org
Grab it from here I suppose.

>>> help(__builtins__.sum)
Help on built-in function sum in module __builtin__:
...

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Roy Smith

unread,
Jan 8, 2014, 3:15:25 PM1/8/14
to
In article <mailman.5194.1389210...@python.org>,
Roy Smith <r...@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.

Peter Otten

unread,
Jan 8, 2014, 3:24:37 PM1/8/14
to pytho...@python.org
Roy Smith wrote:

> I'm working with ipython's pylab mode, which replaces the builtin sum()
> with the one from numpy:
>
> In [105]:
> sum
>
> Out[105]:
> <function numpy.core.fromnumeric.sum>
>
> Is there any way to recover a reference to the builtin sum()?

>>> from numpy import *
>>> sum
<function sum at 0x7fb99782cc08>
>>> del sum
>>> sum
<built-in function sum>

Doing it more than once does no harm:

>>> del sum
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sum' is not defined
>>> sum
<built-in function sum>


Chris Angelico

unread,
Jan 8, 2014, 5:22:52 PM1/8/14
to pytho...@python.org
On Thu, Jan 9, 2014 at 7:15 AM, Roy Smith <r...@panix.com> wrote:
> 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"]

That probably means that it _only_ overrides the one from
__builtins__, which is the easiest way.

You're talking about something that's done at the interactive prompt.
It should be possible to play with sitecustomize.py to snapshot your
original builtins. Try this (untested):

# sitecustomize.py
import builtins
import sys
sys.original_builtins = builtins
sys.original_sum = sum


(or use __builtins__ or __builtin__ or whatever's appropriate for your version)

If snapshotting all of builtins doesn't work, snapshotting just sum
should. The key is executing code before pylab does its overwriting.

ChrisA

Dave Angel

unread,
Jan 8, 2014, 7:02:43 PM1/8/14
to pytho...@python.org
On Wed, 8 Jan 2014 14:52:10 -0500, Roy Smith <r...@panix.com> wrote:
> I'm working with ipython's pylab mode, which replaces the builtin
sum() with the one from numpy:

> In [105]:
> sum

> Out[105]:
> <function numpy.core.fromnumeric.sum>

> Is there any way to recover a reference to the builtin sum()?

goodsum=__builtins__.sum

--
DaveA

0 new messages