Python Learning Notes (vi) Another talk about Python modules

Source: Internet
Author: User

python programs contain multiple module files, one main, top-level file implementation of the main control process, calling components and tools, other module files provide components and tools, Python comes with a lot of useful modules called standard link library. modules are the highest-level program organizational unit in Python that encapsulates code and data for reuse. Each file is a module and can be imported to use variables from other modules (all variables defined at the top level). A module object is generated when the module is imported. From an abstract point of view, the role of the module is:

1) Code reuse-code for module files can be saved permanently, can be used by multiple clients, rerun and reload arbitrarily

2) partition the System namespace-essentially, the module is a package that encapsulates the variable name, and the code and objects are encapsulated inside the module to avoid conflicting variable names. If you do not import the file accurately, another module file is not visible to the variable of a module.

3) Shared services and data-modules are useful for sharing components across systems, and for a global object that is used by multiple functions and files, it is defined in a module file that is imported by multiple clients.


The three important statements and functions of a module are:

Import: To get a module for the importer as a whole, a module only works after the first import, and then uses reload if the module needs to be updated. The client is able to gain read access to the variable name of the imported module, and the client's variable is not visible to the module being imported. The procedure for the first import of a file is:

(1) Search-first Python must find the module file referenced by import, the import statement can only give the name, Python uses the standard module search path to find the corresponding file, the search directory includes: Program home directory, standard link library directory, The contents of the Pythonpath directory (if it is set) and any existing. pth files. The above four components are combined into Sys.path, the first two are automatically defined, and the latter two are customized by the user. Python selects the first file that matches the filename in the search path, and the same directory with the same file name (with a different suffix) loads the first occurrence (left). First search in the main directory (the directory where the top-level file is located or the interactive mode), then the directory of the PYTHONPATH environment variable settings is searched from left to right, then the standard connection library, and finally the custom. PTH catalog file. Import also supports redefinition.

(2) compile (optional)-After the module file is found, Python compares the timestamp to determine whether to compile the file into bytecode. If a bytecode file is found, the byte code is loaded directly.

(3) Run-byte code of the execution module, all statements executed sequentially

From: Allows the importer to obtain a specific variable name from a module, creating a new variable in the client, and only the variable name is copied over. The statement "from model Import *" is when the client obtains all the values in the Model module copy of the variable, that is, the model module's namespace into the client module, but does not import the following underscore "_" variable, if defined "__all__" The variables are read first. No matter how many variables are copied, the From statement always imports the entire module into memory. A potential threat from the from statement is that if a variable with the same name exists in the client, its value is replaced by the imported value, and the old variable value may be imported when used with reload. If you use from to get a specific variable name in a module, you must remember that you can only read variable names that have been assigned in the module.

Reload (): Reload () is a function that forces an imported module to be overloaded and rerun without aborting the program, and the pre-load module must already be imported. With reload, users can immediately see the results of component modifications without having to stop the entire program. When calling reload, Python will reread the module's source file, re-execute its top-level statement, and any place in the client program where the module object is applied will automatically be affected by reload. It is important to note that the variables previously imported through the from are unaffected and remain the old objects before overloading. Furthermore, reload model only overloads the model file and does not reload the other module files in the model file, so the system can be designed to automatically overload subcomponents (that is, add reload calls in each module) or write tools to automate transitive overloads.

Python Learning Notes (vi) Another talk about Python modules

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.