Basic Python program Style

Source: Internet
Author: User

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 __'

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.