从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()
函数实现的?你知道吗
但是https://stackoverflow.com/a/44655619/156458意味着
^默认情况下,{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 ^{} function (基本上
builtins.__import__
是一个轻量级的Python->;C包装器),它包含以下注释:因此,出于性能原因,
import
使用C优化的代码,而不是importlib
的Python实现。这两个实现保持同步,但是,如果要创建一个pull请求,Python核心开发人员也会要求您在接受更改之前更新另一个。你知道吗你强调的用法使我相信你误读了文档;
importlib
并不是import语句的实现。这是Python源代码中import语句的实现,与C代码中import语句的实现相反。你知道吗相关问题 更多 >
编程相关推荐