Python Coding Standards Summary

Here Insert Picture Description

Encoding format statement

In general, the encoding format declaration is required. If the python source code file does not declare the encoding format, python interpreter will use the default ASCII encoding, once the source file containing non-ASCII character encoding, python interpreter will report an error. In UTF-8, for example, the following two statements are in line with the encoding format rules

# -*- coding: utf-8 -*-

indentation

Unified use 4 spaces to indent. Never use tab, tab and space do not mix. In the case of line connection, I generally use 4 spaces hanging indent. E.g:

var_dict = {
    'name': 'xufive',
    'mail': '[email protected]'
}

quotation marks

  1. Natural language use double quotes
  2. Machine identification to use single quotes
  3. Regular expressions use double quotes

Blank line

  1. Encoding format specification, introduced module, two blank lines between the constant and global variable declarations, the definition and execution code top
  2. Blank blank line between the top two lines between definitions, methods defined
  3. Inside the function or method, a blank line where necessary to enhance the sense of rhythm, but should avoid consecutive blank lines

Import module

The total import should be placed on top of the file, located behind the module comments and docstrings, before global variables and constants module. Should be introduced in order from most common to least common packet sequence, a blank line between the packet

  1. Import Standard Library
  2. Import third-party libraries
  3. Application specifies import

Naming conventions

  1. Try to use lowercase module names, initials remain lowercase, try not to use an underscore
  2. Class names use camel (CamelCase) naming style, capitalized, beginning underscore a private class available
  3. Function name all lower case, if multiple words separated by underscores
  4. Private functions are available beginning with an underscore
  5. Try to lowercase variable names, if multiple words separated by underscores
  6. Constant all-uppercase, separated if multiple words, use underscores

Here Insert Picture Description

Published 128 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/lsqzedu/article/details/103749908