Style Guide For Python Code

Start from the beginning
                                        

    - Immediately inside parentheses, brackets or braces.

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

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

    - Immediately before a comma, semicolon, or colon:

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

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

    - Immediately before the open parenthesis that starts the argument

      list of a function call:

      Yes: spam(1)

      No:  spam (1)

    - Immediately before the open parenthesis that starts an indexing or

      slicing:

      Yes: dict['key'] = list[index]

      No:  dict ['key'] = list [index]

    - More than one space around an assignment (or other) operator to

      align it with another.

      Yes:

          x = 1

          y = 2

          long_variable = 3

      No:

          x             = 1

          y             = 2

          long_variable = 3

  Other Recommendations

    - Always surround these binary operators with a single space on

      either side: assignment (=), augmented assignment (+=, -= etc.),

      comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not),

      Booleans (and, or, not).

    - Use spaces around arithmetic operators:

      Yes:

          i = i + 1

          submitted += 1

          x = x * 2 - 1

          hypot2 = x * x + y * y

          c = (a + b) * (a - b)

      No:

          i=i+1

          submitted +=1

          x = x*2 - 1

          hypot2 = x*x + y*y

          c = (a+b) * (a-b)

    - Don't use spaces around the '=' sign when used to indicate a

      keyword argument or a default parameter value.

      Yes:

          def complex(real, imag=0.0):

              return magic(r=real, i=imag)

      No:

          def complex(real, imag = 0.0):

Docstring ConventionsWhere stories live. Discover now