Effective Python Entry II: Follow the PEP8 style specification

Source: Internet
Author: User
Tags instance method

The Python enhancement suggestion, also known as PEP8, is a style guideline for formatting Python code. As long as you have the syntax, you can write Python code whatever you want. However, a consistent style makes your code more affinity and understandable, and facilitates collaboration when shared with other Python programmers in a large community. But even if you only read your code yourself, following this style specification will make it easier to change it later.

PEP8 has a rich list of details on how to write clear and clean Python code. As the Python language evolves, it will continue to be updated. It is well worth reading the entire online protocol (http://www.python.org/dev/peps/pep-0008). Here are some of the statutes you should be sure to follow:

spaces : In Python, spaces are a significant syntax. In code clarity, Python programmers are particularly sensitive to the role of whitespace.

    • For indentation, use a space to replace the TAB key;
    • Each syntax indentation level uses 4 spaces;
    • The length of the line should be 79 characters or less;
    • Long expressions the continuation indentation of the next line should be more than 4 extra spaces at the normal indent level;
    • In a file, functions and classes should be separated by two blank lines;
    • Inside the class, the method should be separated by a blank line;
    • Do not add a space to the index of the list, the function call, or the keyword parameter assignment;
    • Before and after a variable is assigned, there is only one space added.

naming : PEP8 recommends using separate naming styles in different parts of the language. This will make it easier to distinguish the type of each name when reading the code.

    • Functions, variables, and properties should use the Lowercase_underscore format;
    • Protected instance properties should use the _leading_underscore format;
    • Private instance properties should use the __double_leading_underscore format;
    • Classes and exceptions should use the Capitalizedword format;
    • The module-level constants should use the All_caps format;
    • The instance method of a class should use self as the first parameter name (it points to the object);
    • The class method should use the CLS as the first parameter name (which points to the class).

expressions and Declarations : The philosophy of Python is: "There should be only one--and the best--a clear way to do something." "For expressions and declarations, PEP8 is trying to modify this style in its guidelines.

    • Use an inline negation (if A is not B) to replace the negation of the positive expression (if not an is B);
    • Do not detect null values (such as [] or ') by detecting the length (If Len (somelist) = = 0), using if not somelist to assume that the null value implied evaluation is false;
    • Some things that are not null values (such as [1] or ' Hi '), the statement if Somelist is implied to be true for non-null values;
    • Avoid including if statements, for and while loops, and except compound statements in a row, separating them more clearly in multiple rows.
    • Always. The import statement is placed at the top of the file;
    • Always use the name of the module when importing the module, and do not rely on the path to which the current module belongs. For example: Import foo module from bar package, you should use: From bar import foo, not just: import foo;
    • If you have to import relative, use explicit syntax: from. Impor foo;
    • The import in each section should follow the following order: Standard library module, third-party library module, your own module. The import of each word part should be sorted alphabetically.

Note:

The Pylint tool (http://www.pylint.org/) is a popular Python source static parser, Pylint provides automated checks in Python programs for the types of PEP8 style protocols and checks for various other common errors.

Think about it and write it down.
    • When writing Python code, always follow the PEP8 style specification;
    • Collaborating with them in a large Pthon community is a common style;
    • Using a consistent style makes your code easier to modify in the future.

Reprint Annotated Source

Original (English): Effective Python, item 2

(Chinese): http://www.cnblogs.com/reanote/p/effective_python_item_2

Note (hint): Copyright belongs to the original author all, such as infringe your copyright, please email contact me, I will be notified, as soon as possible delete!

Effective Python Entry II: Follow the PEP8 style specification

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.