模块“内置”的“__import__”属性的默认绑定是什么?
What is the default binding to the `__import__` attribute of the module `builtin`?
来自 Python 简而言之
Custom Importers
An advanced, rarely needed functionality that Python offers is the
ability to change the semantics of some or all import and from
statements.
Rebinding __import__
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.
在"You can rebind the __import__
attribute of the module builtin
"中,"the module builtin
"应该改为"the module builtins
"吗?
是"the __import__
attribute of the module builtin
"bound to importlib.__import__
function by default吗?或者 "the module builtin
" 是否提供绑定到其 __import__
属性的默认实现?
是的,那是书中的错字。在 Python 2 中,相同的模块被命名为 __builtin__
(no s
), in Python 3 it is named builtins
.
builtins.__import__
是一个不同于 importlib.__import__
的函数。如果您要重新绑定 builtins.__import__
,请保存参考。
builtins.__import__
是 implemented in C, and essentially calls the C-API PyImport_ImportModuleLevelObject
function.
importlib.__import__
是一个 pure-Python function。 importlib
的 目标 是提供一个纯 python 导入机制的实现,这样它就可以更容易地被黑客入侵,这个功能也不例外。
来自 Python 简而言之
Custom Importers
An advanced, rarely needed functionality that Python offers is the ability to change the semantics of some or all import and from statements.
Rebinding __import__
You can rebind the
__import__
attribute of the modulebuiltin
to your own custom importer function—for example, one using the generic built-in-wrapping technique shown in “Python built-ins” on page 174.
在"You can rebind the
__import__
attribute of the modulebuiltin
"中,"the modulebuiltin
"应该改为"the modulebuiltins
"吗?是"the
__import__
attribute of the modulebuiltin
"bound toimportlib.__import__
function by default吗?或者 "the modulebuiltin
" 是否提供绑定到其__import__
属性的默认实现?
是的,那是书中的错字。在 Python 2 中,相同的模块被命名为
__builtin__
(nos
), in Python 3 it is namedbuiltins
.builtins.__import__
是一个不同于importlib.__import__
的函数。如果您要重新绑定builtins.__import__
,请保存参考。builtins.__import__
是 implemented in C, and essentially calls the C-APIPyImport_ImportModuleLevelObject
function.importlib.__import__
是一个 pure-Python function。importlib
的 目标 是提供一个纯 python 导入机制的实现,这样它就可以更容易地被黑客入侵,这个功能也不例外。