Python coding specification (PEP 8)

Before explaining specific Python coding standard, first look at the code in Figure 1:



FIG 1 two identical function Python code


Two pieces of code in a comparison chart you will find exactly the same when they contain the code, but it is clear that the right of the code written in the format looks more structured than the code segment of the left, it will be relatively easy to read, fun, because it follows the basic Python code written specification.

8 Python using PEP as a coding standard, wherein the PEP is an abbreviation Python Enhancement Proposal (Python enhancement proposals), the style guide 8 represents the Python code. Here are just some of encoding rules to everyone listed in PEP 8 Beginners should strictly abide by:

    1. Each import statement import only one module, to avoid introducing a plurality of modules, for example: Marble repair factory mechanical member
      1. #recommend
      2. import os
      3. import sys
      4. #Not recommended
      5. import os,sys
      The meaning and usage of import will be introduced in the follow-up, you do not have to go into here.
    2. Do not add a semicolon end of the line, do not use semicolons two commands on the same line, for example:
      1. #Not recommended
      2. height = a float ( INPUT ( "Input Height:" )) ; weight = fioat ( INPUT ( "Input Weight:" )) ;
    3. Recommended that each line is not more than 80 characters, if exceeded, it is recommended to use parentheses to implicit linking multi-line content, not recommended to use the backslash \ connect. For example, if a text string can not achieve complete display line, parentheses may be used to separate them displayed, as follows:
      1. #recommend
      2. S = ( "C language Chinese network is China's leading professional website C language programming,"
      3. "Getting Started classic provides C, C language compiler, C language manuals and other language functions." )
      4. #Not recommended
      5. S = "C language Chinese network is China's leading professional web programming language C, \
      6. Provide C Getting Started classic, C language compiler, C language manuals and other language functions. "
      Note that this programming specification applies to absolutely the most part, except for the following two cases:
      • Import module statement is too long.
      • Notes in the URL.
    4. Blank lines with the necessary increase readability, typically defined between the top (as defined function or class) two blank lines, and a blank line between the method definition, certain functions in addition to the position of the partition also It can be a blank line. For example, the right side of FIG. 1 in this code, IF statements with different determined before the code to achieve multiple functions, and therefore may be used herein blank lines separated.
    5. Typically, and in both sides of comma operator function parameters, it is recommended to use a space separated.

Guess you like

Origin www.cnblogs.com/furuihua/p/12539313.html