indentation Each level is indented with 4 spaces. Use vertical implicit indentation in parentheses or use hanging indentation. EXAMPLE: # (hanging indentation) In general, only one layer of indentation # (hanging indent) But in the following situation, you need to add another layer of indentation, and distinguish it from the subsequent statement block. #å³è·Ÿé€€å›žé€€ Error demonstration: The suspension indentation of the # parameter cannot be distinguished from the subsequent indentation of the code block. # å³è§’ä¸é€€é€€é€€,ä¸æŽ¨è Result = some_function_that_takes_arguments( Maximum line width The maximum line width per line is no more than 79 characters General continuation lines can use backslashes Do not use backslashes in parentheses EXAMPLE: # Continued in brackets, try to continue after the operator Def __init__(self, width, height, Blank line Two lines of blank lines are used to split the definition of top-level functions and classes A single blank line is used to split the method in the class definition EXAMPLE: Def method2(): Def method3(): Module import Each module imported should be in a separate line The import order is as follows: (There is a blank line split between the import of each module type, and the order of the modules in each group is sorted in ascending order from top to bottom according to the first letter of the module) Standard library related third-party libraries local libraries EXAMPLE: Error example: # Do not import by initials String Single quotes and double quotes work the same, but they must be guaranteed to exist in pairs and not mixed. EXAMPLE: Spaces in expressions and statements Avoid spaces in parentheses EXAMPLE: Error example: Avoid spaces before commas, colons, and semicolons EXAMPLE: Error example: There must be no spaces before the left parenthesis of the function call EXAMPLE: Error example: Cannot add multiple spaces before and after an operator such as assignment EXAMPLE: Error example: Place a space on both sides of the binary operator. The compound operator involving = ( += , -= , etc.) EXAMPLE: # Operators in parentheses do not require spaces Comment The comment block comment block is usually applied before the code and has the same indentation as the code. Each line begins with '# ', and there is a single space after #. EXAMPLE: Single line comment (you should avoid unnecessary comments) EXAMPLE: Document string Dell latitude 3140,Dell latitude 3140 replacement parts,Dell latitude E3140,Dell latitude 3140 parts S-yuan Electronic Technology Limited , https://www.laptoppalmrest.com
# (vertical implicit indentation) aligns the left parenthesis
Foo = long_funcTIon_name(var_one, var_two,
Var_three, var_four)
Foo = long_funcTIon_name(
Var_one, var_two,
Var_three, var_four)
Def long_funcTIon_name(
Var_one, var_two, var_three,
Var_four):
Print(var_one)
My_list = [
1, 2, 3,
4, 5, 6,
]
Result = some_funcTIon_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', ​​'f',
)
# When using vertical alignment, the first line cannot have parameters.
Foo = long_function_name(var_one, var_two,
Var_three, var_four)
Def long_function_name(
Var_one, var_two, var_three,
Var_four):
Print(var_one)
My_list = [
1, 2, 3,
4, 5, 6,
]
'a', 'b', 'c',
'd', 'e', ​​'f',
)
# Without brackets, using backslashes
With open('/path/to/some/file/you/want/to/read') as file_1, \
Open('/path/to/some/file/being/written', 'w') as file_2:
File_2.write(file_1.read())
Class Rectangle(Blob):
Color='black', emphasis=None, highlight=0):
If (width == 0 and height == 0 and
Color == 'red' and emphasis == 'strong' or
Highlight > 100):
Raise ValueError("sorry, you lose")
If width == 0 and height == 0 and (color == 'red' or
Approval is None):
Raise ValueError("I don't think so -- values ​​are %s, %s" %
(width, height))
The # class's method definition is split with a single empty line, and two lines of empty lines separate the definition of the top-level function and class.
Class A(object):
Def method1():
Pass
Pass
Pass
# Sort by module initials, recursive accordingly
Import active
Import adidas
Import create
#一row Import multiple modules
Import sys, os, knife
Import create
Import active
Import beyond
(It is recommended that sentences use double quotes, words use single quotes, but not mandatory.)
#å•å·å· and double quotes have the same effect
Name = 'JmilkFan'
Name = "Hey Guys!"
Spam(ham[1], {eggs: 2})
Spam( ham[ 1 ], { eggs: 2 } )
If x == 4: print x, y; x, y = y, x
If x == 4 : print x , y ; x , y = y , x
Spam(1)
Dct['key'] = lst[index]
Spam (1)
Dct ['key'] = lst [index]
x = 1
y = 2
Long_variable = 3
x = 1
y = 2
Long_variable = 3
Comparison operators ( == , < , > , != , <> , <= , >= , in , not in , is , is not )
Logical operators ( and , or , not )
a = b
a or b
Name = get_name(age, sex=None, city=Beijing)
# Have to define the param `args(List)`,
# Otherwise will be capture the CLI option when execute `python manage.py server`.
# oslo_config: (args if args is not None else sys.argv[1:])
CONF(args=[], default_config_files=[CONFIG_FILE])
x = x + 1 # Compensate for border