What is Pep
PEP is the abbreviation for Python enhancement proposal, which translates to Python enhancement proposals.
PEP8
Translator: This article is based on the PEP8 version of the last revision of the 2013-08-02 translation, to view the original English, please refer to PEP8
Brief introduction
The coding conventions given in this document are derived from the code in the Python main release standard library. For the C language style guide used in Python's C language implementation, refer to PEP7.
This document and the PEP 257 (document string specification) are all from Guido1 's Python style Guido paper, plus a supplement from Barry's style guide.
As the Python language itself changes, this guide continues to evolve, with new coding conventions identified and old ones discarded.
Many projects have a proprietary coding style guide that, when conflicts occur, should take precedence over project coding specifications.
A code orchestration
1 indent. Indentation of 4 spaces (the editor can do this), no tap, no mix of taps and spaces.
2 The maximum length per line is 79, the line break can use backslashes, preferably with parentheses. The newline point is to hit enter after the operator.
There are two empty lines between the 3 class and the top-level function definition, an empty line between the method definitions in the class, and an empty line between the logically unrelated paragraphs in the function;
Two-document orchestration
1 sequence of Module contents: module description and docstring-import-globals&constants-other definitions. The import part, in accordance with the standard, three-party and their own writing in order to discharge, between the empty line.
2 Do not have multiple libraries in an import, such as import OS, SYS is not recommended.
3 If you use the From XX import xx Reference library, you can omit the ' module. ', it is possible to have a naming conflict, it is necessary to use import xx.
Use of three spaces
general principle, avoid unnecessary spaces.
1 a variety of closing parentheses without spaces.
2 commas, colons, and semicolons do not add spaces before.
3 function. such as func (1).
4 sequence. such as list[2].
5 operators add a space around each other, and do not add spaces in order to align.
6 function default arguments use the assignment character to omit spaces around.
7 do not write multiple statements on the same line, although use '; ' allows.
8 if/for/while statement, even if the execution statement has only one sentence, another line must be started.
four notes
General principle, wrong comments are better than no comments. So when a piece of code changes, the first thing to do is change the comment!
1 comment, add comment before a piece of code. Add a space after ' # '. Between paragraphs with a line interval of only ' # '. For example:
# description:module config.
#
# input:none
#
2 line comment, Add a comment after a line of code. For example: x = x + 1 # Increment x
But this approach is used sparingly.
3 avoid unnecessary comments.
Five Document description
1 Write docstrings for all common modules, functions, classes, methods, not common, but can write comments (on the next line of Def).
2 If the docstring to be wrapped, refer to the following example, see Pep 257
"" "Return a Foobang
Optional Plotz says to frobnicate the Bizbaz first.
"""
Six naming conventions
The overall principle, the new code must be the following naming style, the existing library encoding as far as possible to maintain style.
1 Try to use the lowercase letters ' l ', the uppercase letters ' O ' and other easily confusing letters.
2 The module is named as short as possible, using the all lowercase method, you can use underscores.
3 packages are named as short as possible, using all lowercase methods, and the underscore cannot be used.
The Class 4 is named using the Capwords method, and the classes used within the module use _capwords.
5 exception naming uses the Capwords+error suffix method.
6 Global variables are as effective as possible within the module, similar to static in C. There are two methods of implementation, one is the __all__ mechanism, and the other is the prefix an underscore.
7 The function is named using the all lowercase method, you can use underscores.
8 A constant is named using all uppercase, you can use underscores.
The 9 classes of properties (methods and variables) are named using the all lowercase method, which can be underlined.
the 9 class has 3 scopes for public, Non-public, and subclass APIs that can be interpreted as the public, private, protected,non-public attributes in C + +, preceded by an underscore.
11 Class of attributes if the name of the keyword conflict, the suffix underlined, try not to use the abbreviation and other ways.
12 to avoid naming conflicts with sub-class attributes, prefix two is underlined before some properties of the class. For example: Class Foo declared __a, access, only through foo._foo__a, to avoid ambiguity. If the subclass is also called Foo, there is nothing you can do about it.
methods of class 13 The first argument must be self, and the first parameter of the static method must be CLS.
Seven coding recommendations
The 1 encoding takes into account the efficiency of other Python implementations, such as the high efficiency of the operator ' + ' in CPython (Python), which is very low in Jython, so the. Join () method should be used.
2 Replace ' = = ' as far as possible with ' is ', for example, if X is not None to be superior to if X.
3 using Class-based exceptions, each module or package has its own exception class, which inherits from exception.
do not use bare except,except followed by specific exceptions in the 4 exception.
the code for the try in the 5 exception is as small as possible. For example:
Try:
value = Collection[key]
except Keyerror:
return Key_not_found (key)
Else:
return Handle_value (value)
to better than
Try:
# Too broad!
return Handle_value (Collection[key])
except Keyerror:
# would 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 '): Better than
no:if Foo[:3] = = ' Bar ':
7 Use Isinstance () to compare the type of the object. Like
yes:if isinstance (obj, int): Better than
no:if type (obj) is type (1):
8 Judging the sequence is empty or not empty, there are the following rules
yes:if not seq:
if seq:
Better than
no:if len (seq)
if not len (seq)
9 string do not end with a space.
102 Binary data determine how to use if boolvalue.
Lao Li share: PEP8 Python code specification