重新导入别名/阴影python内置方法 [英] re-import aliased/shadowed python built-in methods

查看:43
本文介绍了重新导入别名/阴影python内置方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果已经运行

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

  1. 导入模块numpy
  2. 将所有导出的名称从模块复制到当前模块

通过从__builtins__重新分配原始值,可以恢复所需功能的情况.

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

这篇关于重新导入别名/阴影python内置方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆