重新导入别名/阴影python内置方法 [英] re-import aliased/shadowed python built-in methods
问题描述
如果已经运行
from numpy import *
然后,内置的all
和其他几个函数将被具有相同名称的numpy
函数遮盖.
then the built-in all
, and several other functions, are shadowed by numpy
functions with the same names.
最常见的情况是发生这种情况(人们没有完全意识到这一点)是从ipython --pylab
开始以ipython --pylab
开头(但您不应该这样做,请使用--matplotlib
,它不会将任何内容导入到您的命名空间,而是设置与gui相关的魔术.)
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?
这是值得做的,因为内置的all
可以处理生成器,而numpy
版本则不能.
This is worth doing because the built-in all
can deal with generators, where as the numpy
version can not.
推荐答案
您可以做到
all = __builtins__.all
语句from numpy import *
基本上可以做两件事
The statement from numpy import *
basically do two separate things
- 导入模块
numpy
- 将所有导出的名称从模块复制到当前模块
通过从__builtins__
重新分配原始值,可以恢复所需功能的情况.
by re-assigning the original value from __builtins__
you can restore the situation for the functions you need.
这篇关于重新导入别名/阴影python内置方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!