python coding standard

PEP8 encoding standard

1 Code writing:

1. Use 4 spaces for advancement, do not use TAB, and do not allow TAB and spaces to be used together

Four spaces when setting the Tab key

2. The maximum length of each line is 79 bytes, and the excess part is wrapped with a backslash

3. There are two blank lines between class and global function definitions, and one blank line between class and global function definitions, and no blank lines in other places.

2 Document layout:

1. The import section is sorted in the order of standard, third party and self-written, with a blank line in between

2. Do not import multiple library modules in one import, such as: import os, sys, time should be written in multiple lines

3. Use import xx as much as possible and use from xx import yy to reference libraries as little as possible, because name conflicts may occur

3 Use of spaces

1 Do not add spaces before various closing brackets

2 commas, colons, do not add spaces before semicolons

3 Do not add a space before the opening parenthesis of the function

4 Add a space to the left and right of the operator, do not add spaces for alignment

5 The assignment operator used for the default parameter of the function saves spaces around it (default value parameter)

6 Do not write multiple sentences on the same line, and try not to use them;

7 If/for/while statement, even if there is only one execution statement, start a new line

 

Complement old-style classes, new-style classes

Old-style class class A:

                        pass

New-style class class A(object):

                        pass

  What we are talking about now is generally the new type of class that is advocated.

Inherit multiple:

                 classA(B,C,D):

           If methods overlap, search from left to right

      Diamond inheritance: similar to breadth-first traversal

                 A.__mro__ Determine which inheritance is successively

                               Determined by the mro algorithm

                        Diamond inheritance is generally not used

     The above is about inheritance priority in new-style classes

          Inheritance precedence in legacy classes uses depth-first traversal

   2 super is the use of the parent class method in the subclass, and it is only available in the new class    

   3 global and nonlocal

nonlocal is new in python3    


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325814168&siteId=291194637