Python module structure and layout, python module Layout

Source: Internet
Author: User

Python module structure and layout, python module Layout

Using modules to Reasonably organize your Python code is a simple and natural method. You should establish a unified and easy-to-read structure and apply it to every file. The following is a very reasonable layout:
(1) start line (Unix)
(2) module documentation
(3) module Import
(4) variable definition
(5) Class Definition
(6) Function Definition
(7) Main Program
Is an internal structure diagram of a typical module.

(1) start line
Generally, the start line is used only in Unix-like environments. With the start line, you can enter only the script name to execute the script without directly calling the interpreter.
(2) module documentation
This section briefly introduces the functions of the module and the meanings of important global variables. You can access these contents through module. _ doc _ outside the module.
(3) module Import
All the modules required to import the code of the current module. Each module is only imported once (when the current module is loaded). The module import code inside the function is not executed unless the function is being executed.
(4) variable definition
The variables defined here are global variables, which can be directly used by all functions in this module. From a good programming style perspective, unless necessary, you should try to use local variables instead of global variables. If you stick to this, your code will not only be easy to maintain, it also improves performance and saves memory.
(5) class definition statements
All classes must be defined here. When a module is imported, the class statement is executed and the class is defined. The document variable of the class is class. _ doc __.
(6) Function Definition statements
The function defined here can be defined through module. function () is accessed externally. When a module is imported, the def statement is executed and the function is defined. The variable in the function documentation is function. _ doc __.
(7) Main Program
This part of code is executed no matter whether the module is imported by another module or directly executed as a script. Generally, there is not much functional code here, but different functions are called according to the execution mode.
Recommended code style: the main program calls the main () function
The main program code is usually similar to the code you saw earlier. Check the value of the Variable _ name _ and then execute the corresponding call.
The code in the main program usually includes variable assignment, class definition, and function definition. Then, check _ name _ to determine whether to call another function (usually the main () function) to complete the functions of this module.
Most Python modules are used for import and call. To directly run a module, call the regression test code of this module.

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.