Python Module Import principle

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/u012422440/article/details/41791433

Today, on the self-taught Python, I take this opportunity to start blogging again, both to consolidate Python's knowledge and to improve the style of writing.

First, the Python module Foundation

The Python module, then, is the highest level of code reuse in the Python language, a partitioning standard for the System namespace, and a tool library file for the top-level file (which is the part that contains the program's main control flow). The implementation model is very simple, and any Python file can be imported as a model via import. The import of Python is not the same as the C or C + + include, for example:

a.py:

[Python]View PlainCopy
    1. message="Test"
    2. def printer ():
    3. Print ("test")

b.py:

[HTML]View PlainCopy
    1. Import a
    2. A.printer ()

By import, you can get the top level of the b.py file through the variable name B to get all the b.py objects defined in the top level , such as Message,printer.

Second, import working principle

The previous reference to Python's import is not equivalent to C or C + +, because import does not insert a file literal directly into another file, at compile time, and import is actually a run-time operation.

When a program imports a specified file for the first time, it performs three steps:

1. Locate the module file.

Note that two points, 1 does not need to add the file path 2 when importing without the need to add the file's suffix name.

Module Search Path:

* Program Home Directory

*pythonpath Directory

* Standard link library directory

*.pth file Directory

The first three estimates are well understood, and the fourth is that Python runs the user to add a valid path to the module search path, which is a row of rows in the. pth file.

2. Compile into bytecode (if necessary).

If the imported file is not compiled into bytecode, Python will explain it, if it already exists. pyc file, Python will check the timestamp of the bytecode, if the time stamp than the source code is old, the program will be newly compiled into a new bytecode, or skip the compilation process.

3. Execute the code of the module to create the object it defines.

The program will say that the imported files are executed from start to finish, and any assignment to the variable name in this process will produce the properties of the resulting module file, but note that the assignment to the variable name must be done at the top level of the module file, for example, using DEF statements to define the function. This defined function property is added to the module file.

Third, Import,from,reload

Import causes a variable name to refer to the entire module object, and we can get or use the module's properties through the module name, while the from statement replicates the variable name to another scope, and we can use the copied variable name directly in the script.

The issue to note is that the import occurs only once, and when the second import, the program does not perform the three steps in import work, but instead removes the module objects that have been created from the Python internal module table.

Reload can be imported from the new module, in the premise of non-interruption of the program, re-execute the import principle of the three steps mentioned in the import module. Reload will execute the new code for the module file in the module's current namespace, allowing the import module file to be executed by the top-level statement to re-assign the variable name. Reload affects all import statements and only affects the FROM statement that is used later, that is, the use of the FROM statement to copy the obtained variable before reload is not affected by reload.

Python Module Import principle

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.