Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

re-import aliased/shadowed python built-in methods

Tags:

python

numpy

If one has run

from numpy import *

then the built-in all, and several other functions, are shadowed by numpy functions with the same names.

The most common case where this happens (without people fully realizing it) is when starting ipython with ipython --pylab (but you shouldn't be doing this, use --matplotlib, which doesn't import anything into your name space, but sets up the gui-related magic, instead).

Once this has been done, is there anyway to call the built-in functions?

This is worth doing because the built-in all can deal with generators, where as the numpy version can not.

like image 284
tacaswell Avatar asked Sep 12 '13 21:09

tacaswell


1 Answers

you can just do

all = __builtins__.all

The statement from numpy import * basically do two separate things

  1. imports the module numpy
  2. copies all the exported names from the module to the current module

by re-assigning the original value from __builtins__ you can restore the situation for the functions you need.

like image 170
6502 Avatar answered Sep 17 '22 12:09

6502