Python的__loader__是什么? [英] Python's __loader__, what is it?
问题描述
我已经看到 __ loader __
这个术语在一些Python文件中不停地浮动,除了一些简短的目的描述之外,我找不到任何文档,但是它们仍然不能提供足够的信息信息使我对它有很好的了解.我所知道的是,它与导入模块有关,除了我完全不知所措.它有什么作用?什么时候使用?怎么可以使用它?
I've seen the term __loader__
floating around some Python files and I can't find any documentation on it aside from a few brief descriptions about it's purpose, but they still don't provide enough information for me to get a good understanding of it. All I know is that it has something to do with importing modules, other than that I'm completely at a loss. What does it do? When is it used? How can I use it if at all?
推荐答案
什么是
__ loader __
?
__ loader __
是由其加载器在导入的模块上设置的属性.访问它应该返回加载程序对象本身.
__loader__
is an attribute that is set on an imported module by its loader. Accessing it should return the loader object itself.
在3.3之前的Python版本中,内置导入机制未设置 __ loader __
.相反,该属性仅在使用自定义加载程序导入的模块上可用.
In Python versions before 3.3, __loader__
was not set by the built-in import machinery. Instead, this attribute was only available on modules that were imported using a custom loader.
但是,由于 PEP 0302 ,此功能在Python 3.3中发生了变化.现在, __ loader __
在每个导入的模块上都可用:
However, this functionality changed in Python 3.3 because of PEP 0302. Now, __loader__
is available on every module that is imported:
>>> # Python 3.3 interpreter
>>> import os
>>> os.__loader__
<_frozen_importlib.SourceFileLoader object at 0x01F86370>
>>>
什么是装载机?
加载程序是查找程序返回的对象.它使用其 load_module()
方法将模块加载到内存中. importlib.abc.Loader
是以下示例加载程序的抽象基类.
A loader is an object that is returned by a finder. It uses its load_module()
method to load a module into memory. importlib.abc.Loader
is an example of an abstract base class for a loader.
什么是取景器?
查找器是使用其 find_module()
的对象>尝试找到模块加载程序的方法. importlib.abc.Finder
是以下示例查找器的抽象基类.但是请注意,不推荐使用 importlib.abc.MetaPathFinder
和> importlib.abc.PathEntryFinder
.
A finder is an object that uses its find_module()
method to try and find the loader for a module. importlib.abc.Finder
is an example of an abstract base class for a finder. Note however that it is deprecated in favor of importlib.abc.MetaPathFinder
and importlib.abc.PathEntryFinder
.
我怎么使用它?
__ loader __
的主要用途是自省.但是,还有其他两种常见用法:
The main use of __loader__
is for introspection. However, there are two other common uses:
__loader__
可用于收集特定模块加载器的数据.
__loader__
may be used to gather data on a specific module's loader.
在3.3之前的Python版本中,可以将 __ loader __
与 hasattr
一起使用,以检查是否已使用内置导入机制导入了模块:
In Python versions before 3.3, __loader__
could be used with hasattr
to check whether or not a module was imported with the built-in import machinery:
>>> # Python 3.2 interpreter
>>> import os
>>> hasattr(os, '__loader__')
False
>>>
如果 hasattr(os,'__loader __')
返回了 True
,则表示 os
模块是使用自定义加载程序导入的.既然没有,则意味着该模块是使用内置的导入机制导入的.
If hasattr(os, '__loader__')
had returned True
, it would mean that the os
module was imported using a custom loader. Since it did not, it means that the module was imported using the built-in import machinery.
注意:由于PEP 0302所做的更改,以上测试在Python 3.3+中将不起作用.
Note: The above test will not work in Python 3.3+ because of the changes made by PEP 0302.
这篇关于Python的__loader__是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!