PEP8 Python encoding specification, pep8python

Source: Internet
Author: User

PEP8 Python encoding specification, pep8python

Code orchestration
1 indent. The indentation of four spaces (the editor can complete this function). Do not use the Tap, or mix the Tap and space.
2. the maximum length of each line is 79. You can use a backslash to wrap a line. It is best to use parentheses. The line feed must be followed by a carriage return.
There are two blank rows between Class 3 and top-level function definitions. One row is empty between class method definitions. One row is empty between non-logical sections in the function. Try not to empty rows elsewhere.

2. Document orchestration
1. Module Content sequence: module description and docstring-import-globals & constants-other definitions. The import part is discharged in the standard, third-party, and self-writing order, with a blank line between them.
2. Do not import multiple libraries in one sentence, such as import OS. sys is not recommended.
3. If you use the from XX import XX reference library, you can omit 'module. ', which may cause name conflicts. In this case, you must use import XX.

Use of three spaces
The general principle is to avoid unnecessary spaces.
1. Do not add spaces before the brackets.
2. Do not add spaces before commas, colons, or semicolons.
3. Do not add spaces before the left brackets of the function. For example, Func (1 ).
4. Do not add spaces before the left parentheses of the sequence. For example, list [2].
5. Add a space between the left and right operators. Do not add a space for alignment.
6. spaces are omitted between left and right of the default parameter value.
7. Do not write multiple statements in the same line, even though ';' is allowed.
8 if/for/while statements, even if the execution statement has only one sentence, it must start with another line.

4. Notes
In general, the comments with errors are not as good as those without comments. So when a piece of code changes, the first thing is to modify the comment!
The comments must be in English. It is best to use a complete sentence. The first letter is in upper case. The end character must be followed by two spaces to start the next sentence. If it is a phrase, the terminator can be omitted.
One annotation added before a piece of code. Add a space after. Paragraphs are separated by rows with only. For example:
# Description: Module config.
#
# Input: None
#
# Output: None
2 lines of comments. Add comments after a sentence of code. For example, x = x + 1 # Increment x
However, this method should be used as little as possible.
3. Avoid unnecessary comments.

5. Document Description
1. Write docstrings for all common modules, functions, classes, and methods. If not, you can write comments (in the next line of def ).
2 If the docstring needs to wrap a line, refer to the following example. For details, see PEP 257.
"" Return a foobang

Optional plotz says to frobnicate the bizbaz first.

"""

Six naming rules
In general, the new Code must follow the naming style below, and the encoding style of the existing library should be maintained as much as possible.
1. Try to Use lowercase letters 'l', uppercase letters 'O', and other confusing letters.
2. The module name should be as short as possible. It should be in all lower case and can be underlined.
3. The package name should be as short as possible. It should be in lower case and cannot be underlined.
Class 4 naming uses CapWords, while class used inside the module uses _ CapWords.
5. Use the CapWords + Error suffix for exception naming.
6. global variables should be valid only within the module, similar to static variables in C language. There are two implementation methods: the _ all _ mechanism and the prefix and underline.
7. You can use underscores (_) to name all functions in lower case.
8. You can use underscores (_) to name constants in uppercase.
Class 9 attributes (methods and variables) are named in lowercase and can be underlined.
Class 9 attributes have three scopes: public, non-public, and subclass APIs. They can be understood as the public, private, protected, and non-public attributes in C ++ with an underscore prefix.
If the attribute of class 11 conflicts with the keyword name, the suffix should be an underscore, and try not to use scaling or other methods.
12 to avoid naming conflicts with subclass attributes, prefix two underscores before some attributes of the class. For example, when _ a is declared in class Foo, only Foo. _ Foo _ a can be used for access to avoid ambiguity. If the subclass is also called Foo, there is nothing to do with it.
The first parameter of class 13 methods must be self, while the first parameter of static methods must be cls.

Seven encoding suggestions
1. Considering the efficiency of other python implementations, for example, the operator '+' is very efficient in CPython (Python), but it is very low in Jython, so it should be used. join.
2. replace '=' with 'is not'. For example, if x is not None is better than if x.
3. Use a class-based Exception. Each module or package has its own Exception class. This Exception class inherits from Exception.
4. Do not use the bare snapshot T in the exception. The snapshot T is followed by the specific snapshot tions.
5. try as few code as possible in the exception. For example:
Try:
Value = collection [key]
Failed t KeyError:
Return key_not_found (key)
Else:
Return handle_value (value)
Better
Try:
# Too broad!
Return handle_value (collection [key])
Failed t KeyError:
# Will also catch KeyError raised by handle_value ()
Return key_not_found (key)
6. Use startswith () and endswith () instead of slice to check the sequence prefix or suffix. For example:
Yes: if foo. startswith ('bar'): Better
No: if foo [: 3] = 'bar ':
7. Use isinstance () to compare the object type. For example
Yes: if isinstance (obj, int): Better
No: if type (obj) is type (1 ):
8. judge whether the sequence is null or not. The following rules apply:
Yes: if not seq:
If seq:
Better
No: if len (seq)
If not len (seq)
Do not end a string with spaces.
If boolvalue is used for binary data determination.

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.