Current location - Quotes Website - Famous sayings - What is python pep8?
What is python pep8?
PEP is the abbreviation of Python Enhancement Proposal, which is usually translated as "Python Enhancement Proposal". Each PEP is a technical document that the Python community guides Python to develop in a better direction, among which Enhancement Proposal No.8 (PEP 8) is a code style guide written for Python language. Although we can write Python code at will on the premise that there is no problem with grammar, in actual development, writing readable code in a consistent style is what every professional programmer should do, and it is also a requirement put forward in each company's programming specification, which is especially important when many people collaborate to develop a project (team development). We can find this document from the PEP 8 link in Python official website. Let's make a brief summary of the key parts of this document.

Related recommendation: Python introductory course

Use of space

1. Use spaces to indicate indentation, not tabs. This is simply unreasonable for people who are used to other programming languages, because most programmers will use Tab to indicate indentation, but you should know that Python does not use curly braces to construct the syntax of a code block like C/C++ or Java. In Python, both branch and loop structures use indentation to indicate which codes belong to the same level, because this Python code is more dependent on indentation and indentation width than many other languages. In different editors, the width of Tab may be 2, 4 or 8 characters, or even other outrageous values. Tabbing indentation can be a disaster for Python code.

2. Each layer of indentation related to grammar is represented by four spaces.

3. The number of characters per line should not exceed 79. If the expression takes up more than one line because it is too long, all lines except the first line should add 4 spaces to the normal indentation width.

4. The definitions of functions and classes should be separated by two blank lines before and after the code.

5. In the same class, each method should be separated by a blank line.

6. There should be a space on the left and right sides of the binary operator, and only one space is needed.

Identifier naming

PEP 8 advocates naming different identifiers with different naming styles in Python, so that when reading the code, we can determine what role the identifier plays in Python by its name (Python's own built-in modules and some third-party modules do not do very well in this respect).

1, variables, functions and attributes should be spelled in lowercase letters. If there are multiple words, connect them with underscores.

2. Protected instance attributes in a class should start with an underscore.

3. The private instance attribute in a class should start with two underscores.

4. The naming of classes and exceptions should capitalize the first letter of each word.

5. Module-level constants should all be uppercase letters, and if there are multiple words, they should be underlined.

6. The instance method of this class should name the first parameter self to represent the object itself.

7. The class method of a class should name the first parameter cls to represent the class itself.

Expression and statement

There is a famous saying in Zen Buddhism of Python (you can use import this to view it): "There should be one-and preferably only one-taboo way to do it." Translated into Chinese, it means "there should be only one exact way to do one thing", and the idea conveyed by this sentence is also ubiquitous in PEP 8.

1. Use negative words in inline form instead of putting negative words in front of the whole expression. For example, it is easier to understand if A is not B than if A is not B.

2. Do not check the length to determine strings, lists, etc. Is None or has no elements. You should look it up in the way of if not x.

3. Even if the if branch has only one line of code, the for loop, except exception catching, etc. Don't write the code with if, for, except, etc. Writing separately will make the code clearer.

4. The 4.import statement is always placed at the beginning of the file.

5. When importing modules, it is better to import sqrt from math than math.

6. If there are multiple import statements, they should be divided into three parts, namely Python standard module, third-party module and user-defined module from top to bottom, and each part should be arranged in alphabetical order according to the module name.