Python's minimalist coding specification

Source: Internet
Author: User

0. Preface

This article is to read the "Python Coding Rule" summarizes the most elite and simple coding specifications, according to each person's preferences, some places will have different choices, I just do the most simple and easy choice for their own, for your reference only.

1. Important Principles

A. It is important to maintain style consistency, but most importantly: know when to disagree
B. Two good reasons to break an established rule:
C. When applying rules can cause code readability to be degraded (readability is high)
D. Breaking rules in order to be consistent with the surrounding code (history left over)

2, the most simple specification

A. Indent only with spaces
B. Using UTF-8 encoding
C. Write only one statement per line
D. Collapse a long line with an end-of-line backslash to limit the maximum of 79 characters per line
E. Import package: Unique, large to small, absolute path per line
F. In-Class methods null 1-line delimited, space-like 2-row delimited
G. operator except *, empty 1 squares on both sides, function parameter = no space around
H. Other modules, functions, methods, variables use all lowercase + underscores except for the class name using the Hump method
I.1 a leading underline is semi-public, 2 leading underscores are private, and a single, post-underline is used to differentiate from reserved words
J. Use Chinese comments during development, and then write English documents when publishing

3. Detailed specification

A. Full-text universal
B. Use only spaces to indent, 4 spaces for 1 indent levels
C. The length of each line is limited to 79 characters, and the long line is collapsed with the end of line backslash
D. Using UTF-8 encoding
E. Write only one statement per line

4. Code naming

Only one package is imported, the order of imports is: standard library, related main package, specific application, 1 rows of blank rows between each set of imports, all imports using the absolute path of the package.

The definition of the split top function and class uses 2 lines of blank lines, the method definition within the split class uses 1 lines, and the class row and the first method definition have 1 lines of blank lines.

The whole use of English writing method to use a space, that is, only the comma, semicolon after the addition of 1 spaces, and any other symbols such as parentheses, brackets, curly braces and so on without spaces to separate symbols and characters, written together to represent a whole; operators are separated by 1 spaces on each side of the symbol except the * number; function parameter = No spaces around the number.

Module name: Not underlined, short, all lowercase;

Class name, exception name: The Hump method of the first letter capitalized word string;

function name, global variable name, method name, instance variable: All lowercase, underlined to increase readability;

A leading underscore is used only for global variables that you do not want to import (there are intrinsic functions and classes) preceded by an underscore), internal methods and instance variables that are not intended to be public interfaces of the class;

Two leading underscores to represent the private name of the class, only to avoid name collisions with the attributes in the class that can be designed for sub-subclasses.

The private attribute must have two leading underscores, no trailing underlines;

A non-public attribute must have a leading underscore, no trailing underline.

Public properties do not have leading and trailing underscores, unless they conflict with reserved words, in which case a single, post-underline is better than a pre-or chaotic spelling, for example: Class_ is better than Klass.

5. Writing Skills

Compared to a single value like None, always use: ' Is ' or ' is not ' to do: If X is not None

Defining base exception classes within modules and packages (Base exception Class)

Use the String method (methods) instead of the string module.

Avoid slicing strings when checking prefixes or suffixes, using startswith () and EndsWith () instead, such as: no:if foo[:3] = = ' Bar ': yes:if foo.startswith (' Bar '):

Comparison of object types using only Isinstance (), such as: no:if type (obj) is type (1): Yes:if isinstance (obj, int)

Determine True or false do not use = =, such as: no:if greeting = = True:Yes:if Greeting:

6. Comments

At development time, comments are all written in Chinese, and when the Script tool is published, the English document is written.

The comment should be a complete sentence (the phrase can also), the first letter capitalized; If the comment is short, omit the end period, and the comment block consists of a paragraph consisting of one or more complete sentences, each sentence ends with a sentence, and two spaces after the end of the sentence.

The comment block starts with # and a space, and the code that follows the comment has the same indentation level, surrounded by a blank line below the comment block.

Use inline comments sparingly, using at least two spaces to separate the statements.

Use Pydoc; Epydoc; Document strings for all public modules, functions, classes, and methods, such as Doxgen, are not necessary for non-public methods, but you should have a comment that describes what this method does, after the "Def" line.

The "" "at the end of a multiline document string should be a separate line.

version Annotation: define a variable __version__ = "$Revision: 1.4 $"

Stay hungry. Stay foolish.

After reading the above, I believe your understanding of Python has deepened a layer. As a python enthusiast, if you encounter confusion in your studies and need to communicate, you can come to our website (http://www.magedu.com/) to get help, learn about the industry's highest rated Linux courses can call: 18519746220.

Python's minimalist coding specification

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.