Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63511 > unrolled thread

Recover handle to shadowed builtin?

Started byRoy Smith <roy@panix.com>
First post2014-01-08 14:52 -0500
Last post2014-01-09 09:22 +1100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Recover handle to shadowed builtin? Roy Smith <roy@panix.com> - 2014-01-08 14:52 -0500
    Re: Recover handle to shadowed builtin? roy@panix.com (Roy Smith) - 2014-01-08 15:15 -0500
      Re: Recover handle to shadowed builtin? Chris Angelico <rosuav@gmail.com> - 2014-01-09 09:22 +1100

#63511 — Recover handle to shadowed builtin?

FromRoy Smith <roy@panix.com>
Date2014-01-08 14:52 -0500
SubjectRecover handle to shadowed builtin?
Message-ID<mailman.5194.1389210743.18130.python-list@python.org>
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
roy@panix.com


[toc] | [next] | [standalone]


#63516

Fromroy@panix.com (Roy Smith)
Date2014-01-08 15:15 -0500
Message-ID<lakbkt$n27$1@panix2.panix.com>
In reply to#63511
In article <mailman.5194.1389210743.18130.python-list@python.org>,
Roy Smith  <roy@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.

[toc] | [prev] | [next] | [standalone]


#63526

FromChris Angelico <rosuav@gmail.com>
Date2014-01-09 09:22 +1100
Message-ID<mailman.5204.1389219775.18130.python-list@python.org>
In reply to#63516
On Thu, Jan 9, 2014 at 7:15 AM, Roy Smith <roy@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web