Reference resources Python Code style of - cloud + Community - Tencent cloud
Catalog
One 、 Format the code
1、 The importance of coding style
2、 Formatting Guide
3、 Indent
4、 governor
5、 Blank line
Two 、 Function writing style
3、 ... and 、 Class coding style
As the programs you write get longer , It is necessary to understand some code formatting conventions . Please take the time to make your code as easy to read as possible ; Making the code easy to read will help you master what the program does , It can also help others understand the code you write . To ensure that everyone writes code that is roughly the same structure .Python Programmers follow some formatting conventions . Learn to write neat Python after , Can understand that others write Python The overall structure of the code ------ As long as they follow the same guidelines as you .
If you want to put forward Python Suggestions for language modification , You need to write Python Improve reporting (Python Enchancement Proposal,PEP).PEP8 It's the oldest PEP One of , It is to Python The programmer provides a code format guide .Python The author of the formatting guide knows , Code is read more than written . When the code is written , You need to read it when debugging ; When adding new features to the program , It takes a long time to read the code ; Share code with other programmers , These programmers will also read them . If you have to choose between making your code easy to write and easy to read ,Python Programmers almost always choose the latter .
PEP 8 Four spaces are recommended for each indentation level , This improves readability , And left enough multi-level indent space . In a word processing document , People often use tabs instead of spaces to indent . For word processing documents , The effect is good , But mixing tabs and spaces makes Python The interpreter is confused . Each text editor provides a setting , The input tab can be converted to a specified number of spaces . You should use tab keys when writing code , But be sure to set up the editor , Make it insert spaces in the document instead of tabs . Mixing tabs and spaces in a program can lead to extremely difficult problems . If you mix tabs and spaces , All tabs in the file can be converted to spaces , Most editors provide this capability .
quite a lot Python Programmers recommend no more than 80 character , When such guidelines were first developed , In most computers , The terminal window can only hold 79 character ; At present , The computer screen can hold much more characters per line , Why use 79 The standard president of characters ? There are other reasons . Professional programmers usually open multiple files on the same screen , Using the standard manager allows them to see the complete lines of each file when they open two or three files side by side on the screen .PEP 8 It is also suggested that no more than 72 character , Because some tools automatically generate documents for large projects , Formatting characters are added at the beginning of each comment line .PEP 8 The president's guide is not an insurmountable red line , Some groups set the maximum president to 99 character . During study , You don't have to think too much about the president of the code , But don't forget , When collaborating on Programming , Almost everyone follows PEP 8 guide . In most editors , You can set up a visual sign ------- It's usually a vertical line , Let you know that the limit you can't cross is above .
Separate the different parts of the program , You can use the blank line . You should use blank lines to organize program files , But it can't be abused . If you have 5 Line to create the list code , also 3 Line to process the list , So it's appropriate to separate the two parts with a blank line . However , You should not use three or four blank lines to separate them . Blank lines don't affect code execution , But it affects the readability of the code .Python The interpreter interprets the code based on horizontal indentation , But don't care about vertical spacing .
When you write a function , A few details need to be kept in mind , Function should be given a descriptive name , And only use lowercase letters and underscores in it . Descriptive names help you and others understand what code is trying to do . The above conventions should also be followed when naming modules .
Each function should contain a comment that briefly describes its function , This comment should follow the function definition , And use the document string format . A good function of the document string allows other programmers to use it simply by reading the description in the document string : They can fully believe that the code works as described : Just know the name of the function 、 The required arguments and the type of return value , You can use it in your own programs .
When a parameter is assigned a default value , No spaces on either side of the equal sign :
def function_name(parameter_0, parameter_1='default value')
For keyword arguments in function calls , This agreement should also be followed :
function_name(value_0, parameter_1='value')
PEP 8(https://www.python.org/dev/peps/pep-0008) It is recommended that the length of the code line not exceed 79 character , This way, as long as the editor window is moderate , You can see the whole line of code . If there are many parameters , The length of the function definition exceeds 79 character , You can enter the left bracket in the function definition and press enter , And press twice on the next line Tab key , Thus, the formal parameter list is distinguished from the function body which is indented by only one layer .
Most editors automatically align subsequent parameter list lines , Make it indent the same as you specified for the first parameter list line :
def function_name(
parameter_0, parameter_1, parameter_2,
parameter_3, parameter_4, parameter_5):
function body...
If a program or module contains more than one function , You can use two empty lines to separate adjacent functions , This makes it easier to know where the previous function ended , Where does the next function start .
be-all import Statements should be placed at the beginning of the file , The only exception is , Comments are used at the beginning of the file to describe the entire program .
You must be familiar with some coding style issues related to classes , This is especially true when your program is complex . The class name shall adopt the hump naming convention ; Capitalize the first letter of every word in the class name , Instead of using underscores . Both the instance name and the module name are lowercase , And underline the words . For each class , You should include a docstring immediately after the class definition . This docstring briefly describes the functionality of the class , And follow the formatting conventions used when writing the documentation string for the function . Each module should also contain a documentation string , What are the classes used for .
You can use blank lines to organize your code , But don't abuse it . In class , You can use a blank line to separate methods ; And in the module , You can use two blank lines to separate classes . When you need to import both the modules of the standard library and the modules you write , First, write the import table accuracy module import sentence , Add another blank line , Then write to import your own module import sentence . The statement contains multiple import Statement in the program , This makes it easier to understand where each module used by the program comes from .