模块"builtin"的"__import__"属性的默认绑定是什么? [英] What is the default binding to the `__import__` attribute of the module `builtin`?
问题描述
从Python入手
自定义进口商
Python提供的一项高级,很少需要的功能是 更改某些或所有导入和从中的语义的能力 声明.
An advanced, rarely needed functionality that Python offers is the ability to change the semantics of some or all import and from statements.
重新绑定__import __
您可以重新绑定模块builtin
的__import__
属性
到您自己的自定义导入程序功能-例如,使用
内建的「Python内建程式」中显示的一般内建封装技术
第174页.
You can rebind the __import__
attribute of the module builtin
to your own custom importer function—for example, one using the
generic built-in-wrapping technique shown in "Python built-ins" on
page 174.
-
在您可以重新绑定模块
builtin
的__import__
属性"中,模块builtin
"应该改为模块builtins
"吗?
In "You can rebind the
__import__
attribute of the modulebuiltin
", should "the modulebuiltin
" be "the modulebuiltins
" instead?
是模块builtin
的__import__
属性"" __import__属性的默认实现?
Is "the __import__
attribute of the module builtin
" bound to importlib.__import__
function by default? Or does "the module builtin
" provide the default implementation bound to its __import__
attribute?
推荐答案
-
是的,这是本书中的错字.在Python 2中,相同的模块命名为
__builtin__
(否s
),在Python 3中被命名为builtins
.
Yes, that's a typo in the book. In Python 2 the same module is named
__builtin__
(nos
), in Python 3 it is namedbuiltins
.
builtins.__import__
是与importlib.__import__
不同的功能.如果要重新绑定builtins.__import__
,请保存参考.
builtins.__import__
is a distinct function from importlib.__import__
. If you are going to rebind builtins.__import__
, save a reference.
-
builtins.__import__
是在C 中实现,并且实际上调用 C- APIPyImport_ImportModuleLevelObject
函数.
builtins.__import__
is implemented in C, and essentially calls the C-APIPyImport_ImportModuleLevelObject
function.
importlib.__import__
is a pure-Python function. The goal of importlib
is to provide a pure-python implementation of the import machinery so it can be hacked on more easily, and this function is no exception.
这篇关于模块"builtin"的"__import__"属性的默认绑定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!