Analysis of python program architecture

Source: Internet
Author: User

Analysis of python program architecture

I. Concepts

Definition: the framework of a common Python program refers to the process of splitting a program into a collection of source code files and connecting these parts together.

The python program architecture can be expressed:

A python program is a module system. It has a top-level file (the program can be run after startup) and multiple module files (used to import the tool library ).

Note: The standard library module is a utility module self-contained in python, also known as the standard link library. There are more than 200 modules in a collection, including common program design tasks unrelated to the platform: operating system interfaces, permanent storage of objects, text matching modes, network and Internet scripts, and GUI construction. Note: These tools are not part of the Python language. However, you can import appropriate modules when standard Python is installed.

Ii. Modules

Concept: a module is the most advanced unit in Python. It encapsulates program code and data for reuse. In fact, every Python file ending with the extension. py is a module.

Three roles of the module:

(1) code reuse;

(2) Division of the system namespace (the module can be understood as the encapsulation of the variable name, that is, the module is the namespace );

(3) share services and data.

Programs and modules: in Python, a program is constructed as a subject and a top-level file. It works with zero or multiple supported files, the latter files can be called modules (top-level files can also be used as modules, but they are generally not used as modules ).

Top-level file: contains the main control process of the program: that is, the file that needs to be run to start the application.

Module files: can be seen as a repository of tools (that is, tools filled with them) that are used to collect top-level files (or other possible places.

Top-level files and module files: the top-level files use tools defined in the module files, and Tools defined by other modules are used for these modules.

Module execution environment: the module contains variables, functions, classes, and other modules (if imported), and the function also has its own local variables. Describes the situation in the module and the interaction with other modules, that is, the execution environment of the module:

It can be seen that modules can be imported, but modules can also be imported and used by other modules. These modules can be written in Python or other languages (such as C.

Iii. import)

Concept: A file can read the content of this module by importing a module (file). That is, the import is essentially to load another file in one file, and can read the content of that file. The content in a module can be used by the outside world through such attributes.

Import is the focus of the program structure in Python.

1. import a module in four ways

  • Import X: import module X, and create a reference for this module in the current namespace (namesapce. You can use X. name to reference the attributes defined in Module X.
  • From X import *: import module X. In the current namespace, create a reference for all public objects (names not starting with _) in the module. That is, you can use a common name (directly name) to reference the attributes in module X. However, X is not defined and cannot use X. name. If the namespace has a name definition with the same name, it will be replaced by a new name.
  • From X import a, B, c: import module X, and create a reference for the specified object in the current namespace.
  • X = _ import _ ('x'): similar to (1) import X. The difference is that X is specified in the current namespace. The usage is the same.

2. What did Python do when importing a module?

When importing a moudle, the Python interpreter checks module registry (sys. moudles) to check whether the module has been previously imported. If sys. if a module already exists (that is, it has been registered), use the existing module object. If sys. modules does not exist:

(1) create a new and empty module object (essentially a dictionary );

(2) Insert the module object into the sys. modules dictionary;

(3) load the object corresponding to the code of this module (if necessary, you can compile it first (compile it into a bit Code )).

Then, in the new module namespace, execute the code object of the module ). All variables specified by the code can be referenced by the module object.

Note: The preceding steps are executed only when the module is executed for the first time. After that, when importing the same module, these steps are skipped, and only the loaded module objects in the memory are extracted. This is an intentional design result. Because importing (finding a file -- compiling it into bytecode -- running Code) is a costly operation, so that each program cannot be repeated more than once. If you want Python to run the file again in the same session (do not stop or restart the Session), you need to call the built-in reload (overload) function (the return value of this function is a Python module object ).

3. import search path order

(1) Main directory of the program: the directory where the program (top-level) files are located (sometimes different from the current working directory (the directory where the Startup Program is located )).

(2) PYTHONPATH (environment variable) Directory

(3) Standard Link Library directory

(4) contents of any. pth file (if any): Find the file in the installation directory and add the required directory as a line.

The above four components are combined into sys. path, which saves the actual configuration of the module search path on the machine. You can view these paths by printing the built-in sys. path list. During import, Python searches for each directory in the list from left to right and finds the corresponding module.

Among them, the search path (1) and (3) are automatically defined by the system, and (2) (4) can be used to expand the path and add it to its source code directory.

In addition, you can also use sys. path to temporarily modify the module search path when Python is running.

For example:

[Python] view plaincopyprint?
  1. Importsys
  2. Sys. path. append ('C: \ mydir ')

Note: The preceding sys. path setting method takes effect temporarily when the program runs. Once the program ends, it will not be retained. The preceding four path configuration methods are permanently saved in the operating system.

References:

1. Chapter 3rd, Chapter 18th, and Chapter 19th of the Python learning Manual

2. Importing Python Modulesfrom effot.org

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.