Note: It is concise and clear to describe the importance of program maintenance.
Document: uses the _ Doc _ special variable to dynamically obtain the document string. The first unassigned string in the module, class declaration, and function declaration can use the property obj. _ Doc _. obj is the name of the module, class, and function.
Indent: the most popular indentation method is four spaces or tab tabs. If the code is cross-platform, different text editors may not set tabs, so try not to use tabs.
Select an identifier name: it is a good habit to define a brief and understandable variable name and module name.
Module structure and layout:
(1) starting line: usually used in Unix/Linux
(2) module document: Describes the module functions and other descriptive strings, which are accessed by module. _ Doc _ outside the module.
(3) module import: import other modules required by the current Module
(4) variable definition: Generally, global variables are defined here. If not necessary, use local variables instead of global variables.
(5) Class Definition: When a module is imported, the class statement is executed and the class is also defined. The class document variable is class. _ Doc __
(6) Function Definition: Define the function through module. function () is externally accessed. 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: code is called when this module is imported or directly executed. Generally, the main program does not write functional code, and all functions in this program are called.
#! /Usr/bin/ENV Python (1) start line
"This is a test module" (2) module documentation
Import sys
(3) module Import
Import OS
DEBUG = true
(4) variable definition
Class fooclass (object ):
(5) Class Definition
"Foo class"
Pass
Def test ():
(6) Function Definition
"Test function"
Foo = fooclass ()
If debug:
Print 'ran test ()'
If _ name __= = '_ main __': (7) Main Program
Test ()
_ Name _ indicates how the module is loaded:
If the template is imported, the value of __name _ is the module name;
If the module is executed directly, the value of __name _ is '_ name __'