默认情况下,`import`语句和`builtin.\uu import()`函数都是基于`importlib.\uu import()`函数实现的吗?

2024-04-18 07:53:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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()函数实现的?你知道吗

但是https://stackoverflow.com/a/44655619/156458意味着 ^默认情况下,{}不是基于importlib.__import__实现的。你知道吗

https://docs.python.org/3/library/functions.html#importbuitlins.__import__函数由import语句调用。因此,如果默认情况下builtins.__import__不是基于importlib.__import__实现的,import语句也不是基于importlib.__import__实现的。你知道吗


Tags: oftheto函数inhttpsorgimport
1条回答
网友
1楼 · 发布于 2024-04-18 07:53:35

不,实际的导入代码是Python代码的端口。请参阅implementation of the ^{} function(基本上builtins.__import__是一个轻量级的Python->;C包装器),它包含以下注释:

/* The below code is importlib.__import__() & _gcd_import(), ported to C
   for added performance. */

因此,出于性能原因,import使用C优化的代码,而不是importlib的Python实现。这两个实现保持同步,但是,如果要创建一个pull请求,Python核心开发人员也会要求您在接受更改之前更新另一个。你知道吗

你强调的用法使我相信你误读了文档;importlib并不是import语句的实现。这是Python源代码中import语句的实现,与C代码中import语句的实现相反。你知道吗

相关问题 更多 >