First, the indentation

Indent 4 Spaces per level.

Continuation lines should be aligned with wrapped elements, either vertically using implicit line concatenation within parentheses, square brackets, curly braces, or indent with hanging lines. When using hang line indentation alignment, consider that the first line should have no arguments, and use indentation to distinguish itself as a continuation line.

Align indent (left and right parentheses aligned)

def long_function_name(var_one, var_two,
                       var_three, var_four):
    print(var_one)
Copy the code

Hanging indent

def long_function_name(
       var_one, var_two,
       var_three, var_four):
   print(var_one)
Copy the code

Levels of indentation

def long_function_name(
      var_one, var_two, var_three,
      var_four):
  print(var_one, var_two, var_three, var_four)
Copy the code

Second, the maximum length of the row

The maximum number of characters for all line limits is 79

Large blocks of text (document characters or comments) without structural restrictions, limited to 72 characters per line.

with open("file1", "r") as f1, \
        open("file2", "r") as f2:
    f2.write(f1.read())
Copy the code

Third, a blank line

Top-level function and class definitions separated by two blank lines.

Class method definitions are separated by an empty line.

class Class01:
    pass
 
 
class Class02:
    def function_01(self):
        pass
 
    def function_02(self):
        pass
Copy the code

4. Naming convention

Variable naming

  • Never use the letters I (lowercase L), O (uppercase O), and I (uppercase I) for single-character variable names.
  • In some fonts, these characters are indistinguishable from the digits 0 and 1. If you want to use I, you can use L instead.

The function name

  • Function names should be lowercase and separated by underscores for better readability.
  • Case mixing is used only in cases where the original case mixing style was primarily used for compatibility, maintaining backward compatibility.

Class name

  • Class names generally use the convention of capitalizing the first letter.
  • In cases where the interface is documented and used primarily for calls, the naming style of the function can be used instead.
  • Note: There is a separate convention for built-in variable naming: most built-in variables are single words (or two words joined together), and uppercase nomenclature is only used for exception names or internal constants.

Class function and method parameters

  • Always use self as the first argument to the instance method.
  • Always use CLS as the first argument to a class method.
  • If a function argument name conflicts with an existing keyword, it is better to underline it at the end rather than abbreviate or spell it freely. So class_ is better than CLSS.

5. String quotes

Single – quoted and double – quoted strings are the same. PEP does not advise on this. Pick a rule and stick to it. When a string contains a single – or double-quoted string, use a different symbol than the uppermost one to avoid backslashes and improve readability.

Module and package import specifications

  • Naming conventions Keep module names short, use lowercase, and avoid special symbols such as dots and question marks
  • Keep module names as simple as possible without separating words (underscores between words are not recommended)

Module Import Suggestions

The sample The results of
from modu import * Poor, it is not clear exactly what was imported from the module
from modu import sqrt Slightly better
import modu Best of all, use modu. SQRT directly to know which module the current method belongs to
import os \n import sys recommended
import os, sys Is not recommended
from subprocess import Popen, PIPE Can also be

Six, packages,

  • Any contains__init__.The directory of the py file is considered a Python package
  • Because the package is imported first__init__.Py files
  • In the package__init__.Py file__all__Function of variables
  • There are global variables in the init.py file__all__Through thefrom xxx import *It only imports when it imports__all__The specified methods and variables, if no default import all.

Seven, comments,

  • Comments that contradict the code are worse than no comments at all. When the code changes, the corresponding comment is updated first!
  • Comments should be complete sentences. If a comment is a phrase or sentence, its first word should be capitalized, unless it begins with a lower-case identifier (never change the case of the identifier!).
  • If the comment is short, the ending period can be omitted. Block comments generally consist of one or more paragraphs of a complete sentence with a period at the end of each sentence.
  • You should use two Spaces at the end of a sentence.
  • Non-english speaking Python programmers, please write comments in English unless you are 120% sure that your code will not be read by speakers of another language.

Block comments

  • Block comments usually apply to some (or all) of the code that follows them, indented to the same level as the code. Block comments start each line with a # and a space (unless block comments indent text internally)
  • Paragraphs inside block comments are usually separated by an empty line of #

Inline comments

  • Use inline comments sparingly
  • Inline comments are comments that accompany code statements. Inline comments and code should be separated by at least two Spaces. Comments begin with a # and a space

Documentation comments

  • Document all common modules, functions, classes, and methods
  • Non-public methods are not necessary, but there should be a comment that describes what the method does. The comment should come after the def line
  • PEP257 describes conventions for writing good documentation notes. In particular, the closing triple quotation mark used for multi-line document comments should be on its own line

Such as:

""" This is the comment comment concrete inner cylinder """Copy the code
For single-line documentation, the trailing triple quotation marks should be on the same line as the documentationCopy the code

Thank you for watching, and I hope this sharing can help you learn. My name is Kikm Ghost, and I like sharing. If you have any questions about Python programming, you can come to me and I am willing to answer them.

Click for answers and information.