PEP8 Python Coding Specification

One code layout
1 indent. 4 spaces of indentation (all editors can do this), no Tap, let alone a mix of Tap and spaces.
2 The maximum length of each line is 79. You can use backslashes for newlines, preferably parentheses. The line feed point is to hit enter after the operator.
3. Two blank lines between class and top-level function definitions; one blank line between method definitions in classes; one blank line between logically irrelevant paragraphs in functions; no more blank lines in other places.

2. Documentation
1 Order of module content: module description and docstring—import—globals&constants—other definitions. The import part is arranged in the order of standard, tripartite and self-writing, with a blank line in between.
2 Do not import multiple libraries in one sentence, such as import os, sys is not recommended.
3 If you use from XX import XX to reference the library, you can omit 'module.', but there may be a naming conflict. In this case, import XX should be used.

Use of three spaces The
general principle is to avoid unnecessary spaces.
1 Do not add spaces before various closing brackets.
2 Do not add spaces before commas, colons, and semicolons.
3 Do not add a space before the opening parenthesis of a function. Such as Func(1).
4 Do not add a space before the opening parenthesis of the sequence. Such as list[2].
5 Add a space to the left and right of the operator, do not add spaces for alignment.
6 Spaces are omitted from left and right of the assignment character used by the default parameter of the function.
7 Do not write multiple statements on the same line, although using ';' allows.
8 In the if/for/while statement, even if the execution statement is only one sentence, it must start a new line.

Four Comments As
a general principle, a wrong comment is better than no comment. So when a piece of code changes, the first thing to do is to modify the comments!
Comments must be in English, preferably a complete sentence, the first letter is capitalized, there must be a terminator after the sentence, and the terminator is followed by two spaces to start the next sentence. In the case of a phrase, the terminator can be omitted.
1 block comment, a comment added before a piece of code. Add a space after '#'. Paragraphs are separated by only '#' lines. for example:

# Description : Module config.
#
# Input : None
#
# Output : None

2 lines of comment, add a comment after a line of code. For example: x = x + 1 # Increment x
But use this method sparingly.
3 Avoid unnecessary comments.

Five Documentation Description
1 Write docstrings for all common modules, functions, classes, and methods; non-common ones are not necessary, but can be commented (on the next line of def).
2 If the docstring needs to wrap, refer to the following example, see PEP 257 for details

"""Return a foobang

Optional plotz says to frobnicate the bizbaz first.

"""

Six naming conventions
As a general principle, the new code must be in the following naming style, and the coding of the existing library should keep the style as far as possible.
1 Try to use lowercase 'l' alone, uppercase 'O' and other easily confusing letters.
2. The module name should be as short as possible, use all lowercase, and underscore can be used.
3 The package name should be as short as possible, using all lowercase, and underscores are not allowed.
4 The naming of the classes uses the CapWords method, and the classes used inside the module adopt the _CapWords method.
5 Exception naming uses the CapWords+Error suffix.
6 Global variables are only valid within the module as far as possible, similar to static in C language. There are two implementation methods, one is the __all__ mechanism; the other is prefixed with an underscore.
7 Use all lowercase for function names, and underscores can be used.
8 Constants are named in all uppercase, and underscores can be used.
9 Class attributes (methods and variables) are named in all lowercase, and underscores can be used.
9 The properties of a class have three scopes of public, non-public and subclass APIs, which can be understood as public, private, protected, and non-public properties in C++, prefixed with an underscore.
11 If the attribute of the class conflicts with the keyword name, suffix it with an underscore, and try not to use other methods such as abbreviation.
12 To avoid naming conflicts with subclass properties, prefix some properties of the class with two underscores. For example, when __a is declared in class Foo, it can only be accessed through Foo._Foo__a to avoid ambiguity. If the subclass is also called Foo, nothing can be done.
13 The first parameter of the method of the class must be self, and the first parameter of the static method must be cls.

Seven coding suggestions
1 Consider the efficiency of other python implementations in coding. For example, the operator '+' is very efficient in CPython (Python), but it is very low in Jython, so the .join() method should be used.
2 Use 'is' and 'is not' instead of '==' whenever possible, eg if x is not None is better than if x.
3 Using class-based exceptions, each module or package has its own exception class, which inherits from Exception.
4 Do not use bare except in exceptions, except followed by specific exceptions.
5 Try as little code as possible in exceptions. for example:

try:
value = collection[key]
except KeyError:
return key_not_found(key)
else:
return handle_value(value)

better than

try:
# Too broad!
return handle_value(collection[key])
except KeyError:
# Will also catch KeyError raised by handle_value()
return key_not_found(key)

6 Use startswith() and endswith() instead of slices for sequence prefix or suffix checking. for example

Yes: if foo.startswith('bar'): is better than
No: if foo[:3] == 'bar':
7 Use isinstance() to compare the types of objects. For example
, Yes: if isinstance(obj, int): is better than
No: if type(obj) is type(1):
8 To judge whether the sequence is empty or not, there are the following rules
Yes: if not seq:
if seq:
is better than
No: if len(seq)
if not len(seq)
9 Do not end the string with spaces.
10 Binary data to determine the way to use if boolvalue.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326272142&siteId=291194637