We all know that the Python code standards that programmers use today is:
I based on the standard, according to their own habits, to adapt, now list the following code habits, easy to understand.
Coding
• Files are encoded in UTF-8 unless otherwise specified
• Unless otherwise specified, the position of the Python translator should be added to the header of the file
• The code takes into account the efficiency of other Python implementations, such as the '+' operator, which is very efficient in CPython (Python) and very inefficient in Jython, so the.join() method should be used.
• Use 'is' and' is not 'instead of' == 'whenever possible, e.g. if x is not None is better than if x.
• With class-based exceptions, each module or package has its own Exception class that inherits from Exception.
• Do not use naked except, except followed by specific exceptions.Try as little code as possible in an exception.
Code Format
-
1-The indentation
Use 4 Spaces for indentation, do not use Tap, and do not mix Tap and Spaces.
-
2-line width
Each line of code should be limited to 80 characters, with backslashes and parentheses preferred for line breaks.A newline point is followed by a return type.In special cases can be slightly more than 80, but the longest shall not exceed 120, a screen can be read, do not need to move around.
-
3-The blank line
Two empty lines between top-level definitions, such as function or class definitions
Method definitions, class definitions and the first method should all have a blank line
Multiple blank lines can be used to separate groups of related functions
Functions can use blank lines to separate logically related code
Try to avoid blank lines elsewhere.
-
4-The blank space
Avoid unnecessary Spaces.
Empty one space on both sides of binary operator [=,-,+=,==,>,in,is not, and]:
No Spaces before closing parentheses, commas, colons, and semicolons, and no Spaces before opening parentheses of functions.Such as Func (1).
Do not place a space before the opening parenthesis of a sequence.If the list [2].Do not add Spaces for alignment.
Function default arguments use assignment operators that omit Spaces.
-
5-The newline
The second line is indented to the beginning of the parentheses.
1. foo = long_function_name(var_one, var_two,
2. var_three, var_four)
Indent the second line with 4 Spaces. This works when the opening parenthesis is a line break.
1. def long_function_name(
2. var_one, var_two, var_three,
3. var_four):
4. print(var_one)
With a backslash \ newline, the binary operator +. Etc should appear at the end of the line;Long strings can also be wrapped this way.
1. session.query(MyTable).\
2. filter_by(id=1).\
3. one()
4.
5. print 'Hello, '\
6. '%s %s!' %\
7. ('Harry', 'Potter')
-
6-Quotes
Natural language uses double quotation marks "...", such as error messages
Machine identifiers use single quotes '...', such as the key in dict
Regular expressions use native double quotes r"..."
DocString (docString) uses three double quotation marks """......"""
-
7-Comment
When a piece of code changes, the first thing you do is change comments!Comments must be in English, preferably in full sentences, with a capital letter and a terminator followed by two Spaces to begin the next sentence.If it is a phrase, you can omit the terminator.
1. Block comment, a comment added before a piece of code.Add a space after the '#'.Paragraphs are separated by only '#' lines.
2. Lines of comment, followed by a comment.X = x + 1 # Increment X
But use it sparingly.
3. Avoid unnecessary comments.
Naming conventions
As a general rule, new code should be named in the following style, and existing library code should be as styled as possible.
- Use the lowercase letter 'L', uppercase letter 'O' and other confusing letters.
- Module name as short as possible, use all lowercase way, can use the underscore.
- The package name should be short. Use all lowercase letters instead of underscores.
- The name of class uses CapWords, big camel name, all the first letters of words are capitalized, and no special characters, underscores and numbers are used. The class used in the module uses _CapWords.
- Name exceptions using CapWords+Error suffixes.
- Global variables are valid only in modules, similar to static in C.There are two implementation methods, one is __all__ mechanism;The second is prefixed with an underscore.Do not use global variables without special requirements
- The function name is all lowercase. You can use underscores, but the underscore cannot start with a letter
- Constant names are all uppercase, can be underlined, start with a uppercase letter, all seen in the project Settings file
- The attributes (methods and variables) of class are named in all lowercase and can be underlined.
- Class attributes have three scopes: public, non-public, and subclass API. They can be understood as public, private, and protected in C++. Non-public attributes are preceded by an underscore.
- If the attribute of class conflicts with the name of the keyword, suffix 1 underline, try not to use abbreviations and other ways.
- To avoid naming conflicts with subclass attributes, prefix some class attributes with two underscores.For example, the Foo class declares __a. Only foo. _Foo__a can be accessed to avoid ambiguity.If the subclass is also called Foo, nothing can be done.
- Class methods must take self as their first argument, while static methods must take CLS as their first argument.The first argument to the static method must be CLS.
The ginseng
- Do not use keywords for variables and parameters
- Do not have a list of default values in the method parameter default values (parameters pass Pointers).
- The method returns True or False first, followed by data, but be sure to return the same type of data, not if return True, else return data.
- It's nerve-racking.