The __loader__ of Python

Source: Internet
Author: User

Main citation: http://stackoverflow.com/questions/22185888/pythons-loader-what-is-it

What is __loader__?

__LOADER__ is the property that is set by the loader on the imported module, which returns the loader object itself when it accesses it.

Prior to Python version 3.3, __loader__ was not set in the built-in import mechanism (without this attribute). Instead, this property applies only to modules imported using the custom loader.

What is loader?

Loader is an object returned by the Finder Finder, which uses its Load_module () method to load a module into memory. For example: Importlib.abc.Loader is an abstract base class for loader.

What is finder?

The Finder is an object that uses its Find_module () method to try to find the loader for a module. For example: Importlib.abc.Finder is the abstract base class for the finder. Note: However it has been deprecated, it is best to use Importlib.abc.MetaPathFinder and Importlib.abc.PathEntryFinder.

How can I use the it,if at all?

The main use of __loader__ is introspection, however, there are two other common uses:

    1. The __loader__ can be used to collect data on specific module loaders.
    2. Prior to Python 3.3, this property could be used to check whether the module was imported by the Hasattr import mechanism.

If hasattr (OS, ' __loader__ ') returns True, this means that the OS module is imported using a custom loader (the built-in import mechanism does not set this property "that is, this attribute is not available"). because it does not, this means that the module is imported using the built-in import mechanism.

The following is a variable that runs de.py import in Python 2.7.9.

The following is a variable that runs demo.py import in Python 3.4.3

Note: The above tests do not work in Python 3.3+ due to changes made by Pep 302.

The __loader__ of Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.