Python Programming specification

Source: Internet
Author: User
An appropriate blank line is useful for increasing the readability of your code, and there are several guidelines for adding a blank line:

1) Add a blank line between the definitions of classes and functions;

2) Add a blank line between the different types of modules in import;

3) Add a blank line between the logical paragraphs in the function, i.e., write the relevant code together as a logical paragraph, separated by a blank line between the paragraphs;

> Break

Although the widescreen display is now capable of displaying more than 256 columns of characters on a single screen, this specification still insists that the maximum length of the line must not exceed 80 characters. There are several ways to collapse a long line:

1) for a long variable name for a short name, such as:

This.is.a.very.long.variable_name = This.is.another.long.variable_name

should read:

variable_name1 = This.is.a.very.long.variable_namevariable_name2 = This.is.another.variable_namevariable_name1 = Variable_name2s

2) Python connects the parentheses, the brackets, and the lines in the braces implicitly, and you can take advantage of this feature. If you want, you can add an extra pair of parentheses around the outside of the expression

3) in a long line to join the continuation of the line break, the position of the line should be in front of the operator, and after wrapping a more indentation, so that the maintenance personnel to see the code at the beginning of the code can be determined that there is a line-breaking, such as:

if color = = White or color = = BLACK \    or color = = BLUE: # Note the OR operator at the beginning of a new line rather than at the end of the line of the old line do_something (color);

> String

1) Avoid accumulating strings with the + and + = operators in the loop. Because the string is immutable, doing so creates unnecessary temporary objects and results in a two-time rather than linear run time. As an alternative, you can add each substring to the list and then use the. Join connection list after the loop ends. (You can also write each substring to a Cstringio.stringio cache)

2) use triple double quotes instead of triple single quotes for multiline strings. Note, however, that implicit row joins are usually clearer, because multiline strings do not conform to the indentation of other parts of the program.

> Naming

Consistent naming can reduce a lot of hassle for developers, and proper naming can significantly improve code readability and reduce maintenance costs.

>> Constants

The constant name is all uppercase letters, and each word is connected by an underscore, such as

White = 0xffffffthis_is_a_constant = 1

>> variables

Variable names are all lowercase, and each word is connected by an underscore, such as

color = whitethis_is_a_variable = 1

Private class members are identified by using a single underscore prefix, multiple definitions expose members, and less defined private members.

Variable names should not have type information, because Python is a dynamic type language. such as Ivalue, Names_list, dict_obj, etc. are not a good name.

>> functions

The function name has the same naming convention as the variable name.

>> class

Words that begin with the class name in uppercase letters (such as capwords, Pascal style) do not use underscores to connect words. Such as:

Class Thisisaclass (object):p

>> Modules

Module name all lowercase, for modules used within the package, you can add an underscore prefix, such as

module.py_internal_module.py

>> Bags

The package's naming convention is the same as the module.

>> Abbreviations

The name should use all spelling words as far as possible, the abbreviation is as follows two kinds of things:

1) commonly used abbreviations, such as XML, ID, etc., should be named with only the first letter, such as

Class Xmlparser (object):p

2) The name contains long words, and a word is abbreviated. You should use the abbreviated method of the contract idiomatic, such as removing the vowel, including the first character of the consonant, for example:

function abbreviation is FN

Text abbreviation for TXT

The object abbreviation is obj

Count abbreviation for CNT

Number is abbreviated to NUM, and so on.

>> Specific naming methods

Mainly refers to the __xxx__ form of the system reserved word naming method. This type of naming can also be used in projects, meaning that variables of this form are read-only and this form of class member functions is not overloaded as much as possible. Such as

Class Base (object):    def __init__ (self, id, parent =none):        self.__id__ = id        self.__parent__ = parent    def __message__ (self, MsgId):        # ... Slightly

Among them, __id__, __parent__ and __message__ all adopt the system reserved word naming method.

>> Import Formats

1) Import order, first import Python built-in modules, and then import third-party modules, and finally import the other modules of their own development projects, these modules are separated by a blank line.

2) Each import should have an exclusive line.

3) Do not use the From module import * Unless it is an import constant definition module or other module where you ensure that there are no namespace conflicts.

> Assign Value

For the assignment language, the main thing is not to do unnecessary alignment, such as

A            = 1                               # This is a line comment variable = 2                               # Another line comment fn           = callback_function    # or line comment

There is no need to do this alignment for two points: one is that this alignment disrupts programming attention, that the brain handles two things at the same time (programming and alignment), and that it is difficult to read and maintain in the future because the horizontal vision of the human eye is very narrow, and it is difficult to see three fields as one row. And adding a longer variable name to the maintenance will also break the alignment. It is best to write this directly:

A = 1 # This is a line comment variable = 2 # another line comment fn = callback_function # or line comment

> Statements

Typically, each statement should have a single row. However, if the test results and test statements are placed on one line, you can also put them on the same line. If this is an if statement, you can do so only if there is no else. In particular, never do this to try/except, because try and except cannot be placed on the same line.

  • 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.