Python Coding specification

Source: Internet
Author: User
Tags instance method

official Python document: https://www.python.org/dev/peps/pep-0008/


Code style:

    • Use 4 empty Glyd to indicate indentation, preferably not using tab, and not to mix the two
    • Wrap at least one line between functions
    • Wrapping at least two lines between classes
    • Dictionary, list, Yongzu elements should add a space before the comma
    • Colon following key in the dictionary: a space should be added between the value and the colon: instead of: between the keys and the key. For example, {' name ': ' Tom '}
    • Longer codes (>79 characters) use \ Wrap. The beginning of the new line should be aligned with the previous delimiter (the argument wrapping is aligned with the left parenthesis)
    • Import is after module comments and docstring, before the constant declaration
    • such as multi-module, please separate import. Like the import OS, SYS is not canonical.
    • The general wording of DocString

"" "Return Foobang


Optional Plotz says to frobnicate the Bizbaz first.

"""

    • Python code files in the following version of 3.0 are recommended to be encoded in Latin characters. More than 3.0 recommended Utf-8
    • There are operator operations in the code, note the precedence, the first action crunch, or parentheses
    • "=", "= =" on both sides with a space such as: if x = = 1:print (' OK '), but in the function or method preferably without spaces

For example, Def func (file= ' D://1.txt ')

    • Try not to put multiple lines on the same line
    • Less use of post-line comments
    • A multiline function comment that begins with # to keep the same indentation as the target function
    • DocString, the first line writes the return value, the next line writes the function function description, then can follow the format >>> func (arg1, arg2, ARG3)/R return value, in order to be able to test.
    • Also, to use source control tools such as Subversion,cvs. After the docstring of the function, write __version__ = "$Revision: $" before code


Naming rules:

  • Use as little as possible ' l ', ' O ', or ' I ', single-letter names as variables.---Tell the truth, it's easy to confuse, it's not clear who is who!
  • Packages and modules should be short and fully lowercase: modulename
  • Name of class name capitalized: ClassName
  • The exception class is named after the error end of the string: Yourerror
  • Global variables are designed to be used within a module, and the __all__ mechanism within the package can be used to remove global variables when using import *
  • Function names are as small as possible and are connected by _ (underscore) between words, such as: Function_name (Arg1, arg2, *args, **kwargs).
  • function and method parameters, self as the first parameter of the instance method, CLS as the first parameter of the class method, if the parameter name is the same as the keyword, in order to avoid, you should add an underscore
  • Method Name & Class Instance property method name rules are the same as function names, class instance private properties and private methods of class instances start with a single underscore _private_fuc_name (self)
  • constant All caps, multiple words with the following dash interval: Total or Constant_var
  • Object-Oriented design recommendations:

    1. If the property in the class instance is set to public or private, consider setting it to private, which is less expensive to modify.
    2. Private cannot be used by third parties because it may be deleted or discarded at any time, the public attribute cannot begin with an underscore
    3. Avoid attributes using large operand operations
    4. If you do not want the property inherited by the quilt class, you should start with a double underscore and not be underlined at the end. This will start Python's naming recognition corrective processing algorithm to ensure that it is not inherited

Coding recommendations:

    • Regardless of the type of Python (cython,jython,iconpython,pypy, etc.), maintain a consistent format. For example, to federate two strings, a = a + B, a + = B, it's better to use '. Join ().
    • None in front of = =, but should be used is or not.
    • With is rather than not

correct: IFA is not None:

Error: If a not isnone:

    • Once it comes to __eq__, __ne__, __lt__, __le__, __gt__, __ge__ compare operations, it's best to implement all 6 implementations instead of just implementing some of them and relying on other functions.
    • Do not use anonymous functions to assign values to a variable, but instead use the DEF function.

correct: Deff (x): Return x*2

error: f = lambda x:x*2

    • Derived exceptions inherit from exception, not baseexception.

Python Coding specification

Related Article

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.