Program code is used to read, improve code readability need to master PEP8 code specification
This requires consistency considerations.
1 About Spaces
Indent using 4 spaces
When the parentheses are swapped, there are three examples to follow
# align the left parenthesis foo = long_function_name (Var_one, Var_two, Var_three, Var_four) # do not align the left parenthesis, but add a layer of indentation, to be different from what is behind. def long_function_name ( var_one, Var_two, Var_three, var_four): print (var_one) # The hanging indent must be indented more than one layer. Foo = Long_ Function_name ( var_one, Var_two, Var_three, Var_four)
Closing parenthesis Fallback
# closing parenthesis Fallback my_list = [ 1, 2, 3, 4, 5, 6,]result = some_function_that_takes_arguments ( ' A ', ' B ', ' C ', ' d ', ' E ', ' f ',)
a line of up to 72 characters , which needs to be wrapped after
There should be no spaces between parentheses and the contents of parentheses
# Avoid spaces inside brackets # Recommended spam (Ham[1], {eggs:2}) # Not recommended spam (ham[1], {eggs:2})
Comma, colon, semicolon split the data should follow the left side of the content, and the right side of the content with a space
# comma, colon, semicolon before avoiding spaces # yesif x = = 4:print x, y; X, y = y, x# noif x = = 4:print x, y; X, y = y, X
There should be no spaces between the colons of the index
binary operators, =, + =,-=, ... and, or before and after a space
+. -Before and after a space, *,/should not have spaces
A list of parameters for a function definition, separated by commas between each parameter, preceded by a space, followed by a space
There should be no spaces before and after the default parameters in the parameter list of the function definition
Like If/else and so on followed by a colon followed by the program block, do not and If/else write in a row
Add a space after the comment #
2 About line breaks
In a class, the class name and the first function interval 2 rows
In a class, the functions in a class are spaced 1 lines apart
3 About importing
A separate import should be followed by a single
From.. Import may not be a single
It is recommended to import with absolute path, not recommended with relative path
Try to avoid wildcard imports, such as using as little as possible *
4 about naming
Module names all use lowercase letters and underline to increase readability
The name of the package is the same as the module, but the package is best not underlined
Class name, first letter capitalization, using hump notation
function name, use all lowercase letters
Global variables, all in uppercase, can be underlined to increase readability
Private variables, preceded by an underscore in the name of the variable
Special variables. Add two underscores before and after a variable's name
Pep8-python Programming Specification