`import`语句和`builtin.__import__()`函数默认都是基于`importlib.__import()`函数实现的吗?
Are both `import` statement and `builtin.__import__()` function implemented based on `importlib.__import()` function by default?
来自https://docs.python.org/3/library/importlib.html
The purpose of the importlib
package is two-fold.
One is to provide
the implementation of the import
statement (and thus, by extension,
the __import__()
function) in Python source code. This provides an
implementation of import
which is portable to any Python interpreter.
This also provides an implementation which is easier to comprehend
than one implemented in a programming language other than Python.
Two, the components to implement import
are exposed in this package,
making it easier for users to create their own custom objects (known
generically as an importer) to participate in the import process.
是不是说import
语句和builtin.__import__()
函数默认都是基于importlib.__import()
函数实现的?
但是 意味着
builtins.__import__
默认不基于importlib.__import__
实现。
https://docs.python.org/3/library/functions.html#import 说
buitlins.__import__
函数由 import
语句调用。所以如果builtins.__import__
默认不基于importlib.__import__
实现,那么import
语句默认也不基于importlib.__import__
实现。
不是,实际导入代码是Python代码的端口。请参阅 implementation of the PyImport_ImportModuleLevelObject()
function(builtins.__import__
基本上是 Python->C 的轻型包装器),其中包含评论:
/* The below code is importlib.__import__() & _gcd_import(), ported to C
for added performance. */
因此,出于 性能原因,import
使用 C 优化代码,而不是 importlib
的 Python 实现。这两个实现保持同步,但是,如果您要创建一个涉及其中一个的拉取请求,Python 核心开发人员会要求您在接受您的更改之前也更新另一个。
你对强调的使用让我相信你误读了文档; importlib
不是 import 语句的实现。这是导入语句在Python源代码中的实现,对比导入的实现C 代码中的语句 .
来自https://docs.python.org/3/library/importlib.html
The purpose of the
importlib
package is two-fold.One is to provide the implementation of the
import
statement (and thus, by extension, the__import__()
function) in Python source code. This provides an implementation ofimport
which is portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.Two, the components to implement
import
are exposed in this package, making it easier for users to create their own custom objects (known generically as an importer) to participate in the import process.
是不是说import
语句和builtin.__import__()
函数默认都是基于importlib.__import()
函数实现的?
但是 builtins.__import__
默认不基于importlib.__import__
实现。
https://docs.python.org/3/library/functions.html#import 说
buitlins.__import__
函数由 import
语句调用。所以如果builtins.__import__
默认不基于importlib.__import__
实现,那么import
语句默认也不基于importlib.__import__
实现。
不是,实际导入代码是Python代码的端口。请参阅 implementation of the PyImport_ImportModuleLevelObject()
function(builtins.__import__
基本上是 Python->C 的轻型包装器),其中包含评论:
/* The below code is importlib.__import__() & _gcd_import(), ported to C
for added performance. */
因此,出于 性能原因,import
使用 C 优化代码,而不是 importlib
的 Python 实现。这两个实现保持同步,但是,如果您要创建一个涉及其中一个的拉取请求,Python 核心开发人员会要求您在接受您的更改之前也更新另一个。
你对强调的使用让我相信你误读了文档; importlib
不是 import 语句的实现。这是导入语句在Python源代码中的实现,对比导入的实现C 代码中的语句 .