Python coding specification (Google)

Python style specification (Google)

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

If you are following the official English version of Google, please move on Google Style Guide

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

semicolon

Do not add a semicolon at the end of the line, and do not use a semicolon to put two commands on the same line.

Row length

No more than 80 characters per line

Except for:

  1. Long import module statement
  2. URL in comment

Do not use backslashes to join rows.

Python will The lines in parentheses, brackets and curly braces are implicitly connected , you can take advantage of this feature If necessary, you can add a pair of extra 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 cannot fit on one line, parentheses can be used to realize implicit line connection:

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

In the comment, place the long URL on one line 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 an explanation in the: ref: ` indentation > ` section of this article

brackets

Use parentheses rather than abuse

Do not use parentheses in return statements or conditional statements except for 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 four spaces

Never use tabs or mix tabs with spaces In the case of line connection, you should either vertically align the elements of the newline (see the example in the section ref: ` line length < line_length > ` or use the hanging indent of 4 spaces (at this time, the first line should not have parameters):

Yes:   # Align with start variable
       foo = long_function_name(var_one, var_two,
                                var_three, var_four)

       # Align with starting value in dictionary
       foo = {
           long_dictionary_key: value1 +
                                value2,
           ...
       }

       # 4 spaces indented, first line not required
       foo = long_function_name(
           var_one, var_two, var_three,
           var_four)

       # Indent 4 spaces in dictionary
       foo = {
           long_dictionary_key:
               long_dictionary_value,
           ...
       }
No:    # Spaces in the first line are prohibited
      foo = long_function_name(var_one, var_two,
          var_three, var_four)

      # Two spaces are prohibited
      foo = long_function_name(
        var_one, var_two, var_three,
        var_four)

      # Indentation is not handled in the dictionary
      foo = {
          long_dictionary_key:
              long_dictionary_value,
              ...
      }

Blank line

There are two lines between top-level definitions and one line between method definitions

There are two empty lines between top-level definitions, such as function or class definitions There should be a blank line between the method definition, class definition and the first method In a function or method, leave a line blank if you think it is appropriate

Space

Use spaces around punctuation in accordance with standard typesetting specifications

There should be no spaces in brackets

Use spaces around punctuation in accordance with standard typesetting specifications

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

Don't put spaces before commas, semicolons, colons, but after them (except at the end of the line)

Yes: if x == 4:
         print x, y
     x, y = y, x
No:  if x == 4 :
         print x , y
     x , y = y , x

There should be no space before the left parenthesis of parameter list, index or slice

Yes: spam(1)
no: spam (1)
Yes: dict['key'] = list[index]
No:  dict ['key'] = list [index]

Add a space on both sides of the binary 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 the arithmetic operator, you need to make a good judgment However, both sides must be consistent

Yes: x == 1
No:  x<1

When '=' is used to indicate keyword parameters or default parameter values, do not use spaces on both sides

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

Do not use spaces to vertically align marks between multiple lines, as this will become a burden of maintenance (applicable to: #, = etc.):

Yes:
     foo = 1000  # notes
     long_name = 2  # Notes do not need to be aligned

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

     dictionary = {
         "foo"      : 1,
         "long_name": 2,
         }

Added by alcibar on Mon, 10 Jan 2022 15:25:57 +0200