Python programming specifications and python Programming
Two reasons for breaking an established rule:
- Applying this rule will cause the code to become less readable.
- To be consistent with the surrounding code.
Encoding:
- All Python script files should be identified by the following mark or its compatible format on the header:
# -*- coding:utf-8 -*-
- Set the editor, Which is saved as UTF-8 by default.
Indent:
- Do not mix tabs and spaces.
- The most popular Python indent method is to use only spaces, followed by only tabs.
- Four spaces are set by default when space indentation is used.
Broken Line:
- The maximum length of a row cannot exceed 79 characters.
- We recommend that you limit the size of a large text (document string or comment) to 72 characters.
- The preferred way to fold long rows is to use Python-supported parentheses, square brackets, and row continuation in curly brackets.
- You can use a backslash to break a line. The position of the line break should be prior to the operator, and an indentation should be added after the line break.
Empty row:
- Use two empty rows to separate functions and classes, and use one empty row to separate class methods.
- Add blank lines between different types of import modules.
Import:
- An import statement is used to import a module.
- It is best not to use from moudle import *.
- Imports is usually placed at the top of the file. Only after the module comment and document string, before the global variables and constants of the module.
- Import order: first import the Python built-in module, then import the third-party module, and finally import other modules in the self-developed project.
Space:
- Add spaces before and after Binary Arithmetic Operators and logical operators.
- Parentheses (including parentheses, square brackets, and curly brackets) are left with no spaces.
- Comma, colon, and semicolon are not preceded by a space, followed by a space (the colon does not need to be followed by a space ).
- When the equal sign is used to specify a parameter or default parameter value, do not add spaces on both sides.
Name:
- Do not use lowercase letters l, uppercase letters O, and uppercase letters I as the variable name for a single character.
- All the letters of a constant name are capitalized and each word is connected with an underscore.
- The variable name and function name are all lowercase letters, and each word is connected with an underscore. The variable name should not contain type information.
- The module name and package name must contain lowercase letters, which are short and underline-free.
- The class name uses a string of uppercase letters without underscores.
Branches and loops:
- Do not write multiple statements on the same line.
- Use the else clause of loop statements to simplify code.
Note:
- There are two types of Annotations: one is the "true" annotation starting with #, and the other is the document string. The former shows why the current implementation is selected and the principles and difficulties of this implementation. The latter shows how to use this package, module, class, function (method), and even use examples and unit tests.
- Adhere to the principles of appropriate annotations. Do not comment on codes that do not have any technical difficulties. You must comment on codes that do not have any technical difficulties. When the code is modified, the comments should always be updated first.
- A comment should be a complete sentence. Each sentence ends with a full ending. If the comment is short, the end of the comment is omitted.
- If a comment is a phrase or sentence, the first letter should be in upper case unless it is an identifier starting with a lowercase letter.
- The comment block should have the same indentation level as the code. Each line in the comment block starts with '#' and a space (unless it is the indent text in the comment ). The paragraphs in the comment block are separated by rows that contain only a single. The upper and lower sides of the comment block should be surrounded by empty rows.
- Line comments should be separated by at least two spaces and the statement. They should start with '#' and a single space.
Document string:
- Compile document strings for all public modules, functions, classes, and methods. The document string is not necessary for non-public methods, but a comment must be provided to describe the method. This comment should be placed after the "def" line.
- "" At the end of a multi-line document string should be arranged separately.
Others:
- The comparison of object types should always replace the direct comparison type with isinstance.
if isinstance(obj, int):
- Do not use "=" to compare the Boolean value to determine whether it is True or False, for example:
if greeting == True:
It should be written:
if greeting: