Python coding specification and python Encoding

Source: Internet
Author: User

Python coding specification and python Encoding

Python official documentation: Https://www.python.org/dev/peps/pep-0008/


Code style:

  • 4 spaces are used to indicate indentation. It is best not to use tab, or mix the two.
  • A line feed between functions must be at least one line.
  • There must be at least two lines of lines between classes.
  • The dictionary, list, and ancestor should contain a space before the comma.
  • The colon following the key in the dictionary: a space should be added between the value and the colon, instead of before the key. For example: {'name': 'Tom '}
  • Long Code (> 79 characters) with \ line feed. The starting position of the new line should be aligned with the previous separator after the line feed (that is, the line feed is aligned with the left parenthesis ).
  • Import is located after module comments and docstring, before the constant Declaration
  • For multiple modules, Please import them separately. For example, import OS, sys is not standard.
  • Conventional Docstring writing

"" Return foobang


Optional plotz says to frobnicate the bizbaz first.

"""

  • We recommend that you use latin character encoding for code files of Python versions earlier than 3.0. UTF-8 is recommended for more than 3.0.
  • There are operator operations in the Code. Please pay attention to the priority, the first operation is tightening, or brackets
  • "=", "=" Add a space on both sides, for example, if x = 1: print ('OK'), but it is best not to use a space in a function or method.

For example: def func (file = 'd: // 1.txt ')

  • Try not to put multiple lines of code in the same line
  • Comment after less rows
  • Comments of multiple functions starting with # To keep the same indentation as the target function
  • Docstring: the return value is written in the first line, and the function description is written in the next line. You can return values in the format >>> func (arg1, arg2, arg3)/r to facilitate testing.
  • In addition, to use source code management tools such as subversion and cvs. You can write _ version _ = "$ Revision: $" before the code after the docstring of the function"


Naming rules:

  • Try to use less 'l', 'O', or 'I', and use a single letter as the variable name. --- to be honest, it is easy to confuse and clarify who is who!
  • The package and module should be short and all lowercase letters: modulename
  • Class name in the upper-case format: ClassName
  • For exception classes, the character string ending with "Error" should be named YourError.
  • Global variables should be designed only for use in modules. When you use import *, the _ all _ mechanism in the package removes global variables.
  • Function names should be in lowercase and words should be connected with underscores (_). For example, function_name (arg1, arg2, * args, ** kwargs)
  • Function and method parameters. self is the first parameter of the instance method, and cls is the first parameter of the class method. If the parameter name is the same as the keyword, it should be underlined to avoid it.
  • Method Name & class instance attributes method name rules are the same as function names, class instance private attributes and class instance private methods start with a single underline _ private_fuc_name (self)
  • Constants are all capitalized. If multiple words are used, the following strip interval is used: TOTAL or CONSTANT_VAR.
  • Object-oriented design suggestions:

Encoding recommendations:

  • No matter which type of Python (Cython, Jython, IconPython, PyPy, etc.), the format is consistent. For example, to combine two strings, a = a + B and a + = B, it is better to use ''. join.
  • Do not use = before None, but use is or is not.
  • Use is not instead of not is

Correct: ifa is not None:

Error: if a not isNone:

  • If _ eq _, _ ne _, _ lt _, _ le _, _ gt _, _ ge, it is best to implement all six functions, instead of only implementing some of them, instead of relying on other functions.
  • Instead of assigning values to a variable using an anonymous function, use the def function.

Correct: deff (x): return x * 2

Error: f = lambda x: x * 2

  • The derived Exception is inherited from Exception rather than BaseException.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.