Python coding specifications

Source: Internet
Author: User
Document directory
  • Python coding specifications
Python coding standard indent

Use four spaces as the first indent.

Tab or space

Do not mix tabs and spaces.

Maximum row length

The maximum length of a row is limited to 80 characters.

Blank line

Separates top-level functions and class definitions using two blank rows. The method definition in the class is separated by a blank line. Extra empty rows are used to separate related function groups. Use empty rows to separate logical blocks.

Space in a statement or expression
x = 1y = 2  
Note

It is best to write comments in English. The comments that violate the code are not as good as the comments without comments. If the comment is a sentence, the first letter must be capitalized. Add two spaces at the end of a sentence.

  • Block comment

    Start with # and end #

  • Internal comment
Document

All functions and modules should have documents, as shown in the following example:

def kos_root():    """Return the pathname of the KOS root directory."""    global _kos_root    if _kos_root: return _kos_root    ...def complex(real=0.0, imag=0.0):    """Form a complex number.    Keyword arguments:    real -- the real part (default 0.0)    imag -- the imaginary part (default 0.0)    """    if imag == 0.0 and real == 0.0: return complex_zero    ...  
Version Information

To add the version information of the source code, you can write the following code:

__version__ = "$Revision: 6104 ___FCKpd___2quot;# $Source$

This code is added after the module documentation.

Module

The module name must be in lowercase, because the module corresponds to the file name, which is consistent with the file naming rules.

Class

The class name uses the capwords convention. The class used internally is prefixed with an underscore.

Exception name Function

Use lower_case_with_underscores to name the function.

Variable

Variables that do not need to be exported must be underlined.

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.