Python coding standard based on Google

  • 2021-11-24 02:21:25
  • OfStack

Directory Python Style Specification (Google) Semicolon Line Length Parentheses Indent Blank Line Spaces

Python Style Specification (Google)

This project is not an Google official project, but created and maintained by domestic programmers with enthusiasm.

If you are interested in the official English version of Google, please go to Google Style Guide

In the following code, Yes means recommended and No means not recommended.

Semicolon

Don't add a semicolon at the end of a line, and don't use a semicolon to put two commands on the same line.

Line length

No more than 80 characters per line

Except in the following cases:

URL in long import module statement comments Do not use backslashes to connect rows.

Python implicitly joins lines in parentheses, braces, and curly braces. You can take advantage of this feature.

If necessary, you can add an extra pair of parentheses around the expression.


 Recommend : foo_bar(self, width, height, color='black', design=None, x='foo',
             emphasis=None, highlight=0)
 
     if (width == 0 and height == 0 and
         color == 'red' and emphasis == 'strong'):

If a text string can't fit on a line, you can use parentheses to implement implicit line join:


x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')

In the comment, put the long URL on line 1 if necessary.


Yes:  # See details at
      # http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.html

No:  # See details at
     # http://www.example.com/us/developer/documentation/api/content/\
     # v2.0/csv_file_name_extension_full_specification.html

Note the element indentation in the above example; You can find this article in: ref : ` Indent < indentation > ` Partially find an explanation.

Brackets

Use brackets rather than shortage

Do not use parentheses in return statements or conditional statements unless it is used to implement row joins.

However, it is ok to use parentheses around tuples.


Yes: if foo:
         bar()
     while x:
         x = bar()
     if x and y:
         bar()
     if not x:
         bar()
     return foo
     for (x, y) in dict.items(): ...

No:  if (x):
         bar()
     if not(x):
         bar()
     return (foo)

Indent

Indent the code with 4 spaces

Never use tab, and never mix tab with spaces.

In the case of line join, you should either vertically align the newline elements (see: ref : ` Line length < line_length > ` Examples of the section),

Or use a 4-space suspended indentation (line 1 should not have parameters at this time):


Yes:   #  Align with the starting variable 
       foo = long_function_name(var_one, var_two,
                                var_three, var_four)
        #  Align with the starting value in the dictionary 
       foo = {
           long_dictionary_key: value1 +
                                value2,
          ...
       }
        # 4  Spaces are indented, and the first 1 Row not required 
       foo = long_function_name(
           var_one, var_two, var_three,
           var_four)
 
       #  In a dictionary  4  Spaces indent 
       foo = {
           long_dictionary_key:
               long_dictionary_value,
           ...
       }

No:    #  No. 1 1 Spaces on lines are forbidden 
      foo = long_function_name(var_one, var_two,
          var_three, var_four) 
      # 2  Spaces are forbidden 
      foo = long_function_name(
        var_one, var_two, var_three,
        var_four) 
      #  Indentation is not processed in the dictionary 
      foo = {
          long_dictionary_key:
              long_dictionary_value,
              ...
      }

Blank line

Two empty lines between top-level definitions and one empty line between method definitions

There should be two empty lines between top-level definitions, such as function or class definitions. Method definitions, class definitions and method 1 should all be one empty line.

In some parts of a function or method, if you think it is appropriate, leave one line blank.

Space

Use the spaces on both sides of punctuation according to the standard typesetting specifications

Do not have spaces in parentheses.

Use the spaces on both sides of punctuation according to the standard typesetting specifications


Yes: spam(ham[1], {eggs: 2}, [])

No:  spam( ham[ 1 ], { eggs: 2 }, [ ] )

Do not precede commas, semicolons, and colons with spaces, but should follow them (except at the end of the line).


x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')
0

x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')
1

There should be no space before the opening parenthesis of a parameter list, index, or slice.


x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')
2

no: spam (1)

x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')
4

x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')
5

Add a space on both sides of the 2 yuan operator,

Such as assignment (=), comparison (= =, < , > , !=, < > , < =, > =, in, not in, is, is not), Boolean (and, or, not).

As for how to use the spaces on both sides of arithmetic operators, you need to judge for yourself. No

Be sure to keep 1 on both sides.


x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')
6

x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')
7

When '=' is used to indicate keyword parameters or default parameter values, do not use spaces on either side.


Yes: def complex(real, imag=0.0): return magic(r=real, i=imag)

x = (' This is 1 A very long, very long, very long  '
     ' Very long, very long, very long, very long, very long string ')
9

Do not use spaces to vertically align tags between lines, as this can be a maintenance burden (apply to:, #, =, etc.):


Yes:
     foo = 1000  #  Notes 
     long_name = 2  #  Comments do not need to be aligned 
     dictionary = {
         "foo": 1,
         "long_name": 2,
         }

No:
     foo       = 1000  # Notes
     long_name = 2     # Comments do not need to be aligned
     dictionary = {
         "foo"      : 1,
         "long_name": 2,
         }

The above is based on Google Python coding specification standard details, more information about Google Python coding specification please pay attention to other related articles on this site!


Related articles: