A code orchestration
- Indent. Indentation of 4 spaces (the editor can do this), no tap, no mix of taps and spaces.
- 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 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
- Module Content Order: 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.
- Do not have multiple libraries in an import, such as import OS, SYS is not recommended.
- 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
Principles to avoid unnecessary spaces.
- Do not add spaces before the various closing brackets.
- Do not add spaces before commas, colons, and semicolons.
- Do not add a space before the opening parenthesis of the function. such as func (1).
- Do not add a space before the opening parenthesis of the sequence. such as list[2].
- Add a space around the operator, and do not add spaces in order to align.
- The function default argument uses an assignment character that omits spaces left and right.
- Do not write multiple statements on the same line, although use '; ' allows.
- In a if/for/while statement, you must have a different row even if the execution statement has only one sentence.
Four notes
Principle, the wrong comment is better than no comment. So when a piece of code changes, the first thing to do is change the comment!
Comments must be in English, preferably a complete sentence, the first letter capitalized, after the sentence to have Terminator, the Terminator followed by two spaces, start the next sentence. If it is a phrase, you can omit the terminator.
- Block comments, added comments before a piece of code. Add a space after ' # '. Between paragraphs with a line interval of only ' # '. Like what:
# description:module CONFIG.
#
# Input:none
#
# Output:none
- Line comment, and comment after a sentence. Example: x = x + 1 # Increment X
But this approach is used sparingly.
- Avoid unnecessary annotations.
Five Document description
- Write docstrings for all common modules, functions, classes, methods, not common, but can write comments (on the next line of Def).
- If docstring to wrap, refer to the following example, see Pep 257
"" "Return a Foobang
Optional Plotz says to frobnicate the Bizbaz first.
"""
Six naming conventions
Principle, the new code must be in the following naming style, the existing library encoding as far as possible to maintain style.
- Try to use lowercase letters ' l ', uppercase letters ' O ', and so on to be easily confused.
- The module is named as short as possible, using the all lowercase method, you can use underscores.
3.: Package naming is as short as possible, using all lowercase methods, you cannot use underscores.
- The class is named using Capwords, and the classes used inside the module take the _capwords approach.
- The exception name uses the Capwords+error suffix method.
- 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.
- The function name uses all lowercase, and you can use underscores.
- The constant name is used in all uppercase, and you can use underscores.
- The properties of the class (methods and variables) are named using the all lowercase method, and you can use underscores.
- The properties of the class have 3 scopes public, Non-public, and subclass APIs that can be interpreted as the public, private, protected,non-public attributes in C + +, preceded by an underscore.
- If the attribute of the class conflicts with the keyword name, the suffix is underlined, try not to use other methods such as thumbnails.
- 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.
- The first parameter of a class's method must be self, and the first parameter of a static method must be CLS.
Seven coding recommendations
- The coding 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.
- If possible, replace ' = = ' with ' is ' isn't, such as if X is not None to be superior to if X.
- With 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 exception.
- The code for the try in the exception is as few as possible. Like what:
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: # Will also catch KeyError raised by handle_value() return key_not_found(key)
- The sequence prefix or suffix is checked using startswith () and EndsWith () instead of slices. Like what:
Yes:if foo.startswith (' Bar '): Better Than
No:if Foo[:3] = = ' Bar ':
- Use Isinstance () to compare the type of the object. Like what
Yes:if isinstance (obj, int): Better Than
No:if type (obj) is type (1):
- 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)
- String do not end with a space.
- Binary data determines how to use if boolvalue.
PEP 8 Python Programming specification