Unified and well-designed code specifications, is a good programming habit.
Pycharm This Python IDE uses the famous PEP8 code specification, we will see that when there are code that does not conform to the specification, the compiler gives a hint with a wavy gray line, and this article tells you how to write at least the beautiful code style without the gray warning line.
Indentation (indentation)
Use four spaces to represent each indentation level.
Yes
# align
foo = Long_func_name (Var_one, Var_two,
Var_three, Var_four) with the open separator (opening delimiter)
Use more indentation to differentiate it from other code units
# in the following example, the parameter portion is four more indents than the function body and distinguishes the function body
def long_func_name (Var_one,
var_two, Var_three,
var_four)
Print (Var_one)
# hanging indent (hanging indents): Add a indent level
foo = long_func_name (
var_one, Var_two,
var_ Three, Var_four)
# or:
foo = long_func_name (
var_one, Var_two, Var_three
, Var_four
)
A = [1, 2, 3,
4, 5, 6]
a = [
1, 2, 3,
4, 5, 6
]
No
# When vertical alignment is not applied, the parameter # is not used in the first line
# in other words, you can use parameter Foo = Lone_func_name in the first row when vertically aligned
(Var_one, Var_two,
Var_three, Var_ Four)
# Add a indent level
def long_func_name (
var_one, Var_two, Var_three
var_four) When
indentation is not sufficient to differentiate code structure: Print (Var_one)
maximum length of the Governor
The maximum length of all rows is 79 characters.
With open (') as file_1, \
open (') as file_2:
file_2.write (File_1.read ())
Use the correct line wrapping position. The recommended location is after the two-dollar operator (binary operator, such as and, or, and% in the following code), not before them:
Class Rectangle (Shape):
def __init__ (self, width, height,
color= ' black ', Emphasis=none, highlight=0):
if (width = = 0 and height = 0 and
color = ' red ' and emphasis = ' strong ' or
height >):
Raise ValueError ( "Sorry, you Lose")
if width = = 0 and height = 0 and (color = = ' Red ' or emphasis is None):
Raise ValueError ("I Don ' t-so-values are%s,%s '%
(width, height)
shape.__init__ (self, width, height, color,
emphasis, hig Hlight)
Blank Line
A top-level function (the first function in the current file) or a top-level class (the first class in the current file) must have two blank rows
There is a blank line between functions defined within a class (member functions)
You can use extra empty lines (but be mindful of moderation) to differentiate between groups of functions,
Do not use empty lines between a stack of functions that have only one row (for example, NULL implementations of some functions)
Use a blank line inside a function to identify different logical unit imports import different packages into separate rows
Yes
Import SYS
import OS
No
Import sys, OS
But adding a different module or function from one package is also allowed:
From subprocess import Popen, PIPE
The import file should always be in the header of the file, only after module notes and documents, before the module's global variables and constants
Import files in order:
1. Standard library (such as SYS, OS)
2. Related third party libraries (e.g. NumPy, pandas, Matplotlib)
3. Custom. py files or custom libraries
Identifying the three import files as a group, or using a blank line partition, is recommended for absolute path inclusion because it is more readable and error-prone.
Import mypkg.sibling from
mypkg import sibling from
mypkg.sibling Import Example
However, external relative inclusion is also an acceptable replacement, especially when dealing with complex packet hierarchies, i.e. absolute inclusion will create unnecessary complexity.
From. Import sibling from
. Sibling import Example #. Represents the current path
The standard library does not have a complex package hierarchy and should always use its absolute import path. You should avoid wildcard import file strings
In Python, no distinction is made between single and double quotes, and the Code specification for PEP is not recommended. Any choice, unified use can be. However, when a string contains either single or double quotes, the use of an escape character (\) is avoided in a different way to improve readability. spaces in an expression
Avoid using spaces under the following conditions: Precede curly braces, brackets, and parentheses
Yes
Spam (Ham[1], {eggs:90})
No
Spam (ham [1], {eggs:2})
Tightly attached to commas, semicolons, colons
Yes
if x = = 4:print x, y; X, y = y, X
NO
Colon in a slice
A colon in a slice can be treated as a two-dollar operator (binary operator), with a number of spaces applied on both sides.
Yes
Ham[1:9], Ham[1:9:3], Ham[:9:3], Ham[1::3], Ham[1:9:]
ham[lower:upper], Ham[lower:upper:], Ham[lower::step]
Ham[lower+offset:upper+offset]
ham[: UPPER_FN (x): Step_fn (x)], ham[:: STEP_FN (x)]
Ham[lower + offset:upper + Offset
No
Ham[lower + offset:upper + offset]
Ham[1:9], Ham[1:9], Ham[1:9: 3]
ham[lower:: Upper]
ham[: Upper]
Other suggestions
Always use a single space on both sides of the two-dollar operator: assignment: = Increment Assignment: = = Comparison: = =,!=, <>,;, Boolean: And, or, not
Do not use spaces when used to identify a keyword parameter (using a function) or a default parameter assignment (define function)
Yes
def complex (real, imag=0.): Return
Magic (r=real, I=imag)
No
def complex (real, imag = 0.): Return
Magic (r = Real, I = imag)
Document
Write documentation for all public modules or functions, classes, and methods. You do not have to write Doc documents for Non-public methods, but you should have an annotation that describes the functionality of the algorithm, which should appear after def
End of "" should be exclusive line
"" "Return a Foobang
Optional Plotz says to frobnicate the Bizbaz the" "" "
References
[1] <pep 0008–style Guide for Python code>