Style Guide For Python Code

Start from the beginning
                                        

              return magic(r = real, i = imag)

    - Compound statements (multiple statements on the same line) are

      generally discouraged.

      Yes:

          if foo == 'blah':

              do_blah_thing()

          do_one()

          do_two()

          do_three()

      Rather not:

          if foo == 'blah': do_blah_thing()

          do_one(); do_two(); do_three()

    - While sometimes it's okay to put an if/for/while with a small

      body on the same line, never do this for multi-clause

      statements.  Also avoid folding such long lines!

      Rather not:

          if foo == 'blah': do_blah_thing()

          for x in lst: total += x

          while t < 10: t = delay()

      Definitely not:

          if foo == 'blah': do_blah_thing()

          else: do_non_blah_thing()

          try: something()

          finally: cleanup()

          do_one(); do_two(); do_three(long, argument,

                                       list, like, this)

          if foo == 'blah': one(); two(); three()

Comments

    Comments that contradict the code are worse than no comments.  Always make

    a priority of keeping the comments up-to-date when the code changes!

    Comments should be complete sentences.  If a comment is a phrase or

    sentence, its first word should be capitalized, unless it is an identifier

    that begins with a lower case letter (never alter the case of

    identifiers!).

    If a comment is short, the period at the end can be omitted.  Block

    comments generally consist of one or more paragraphs built out of complete

    sentences, and each sentence should end in a period.

    You should use two spaces after a sentence-ending period.

    When writing English, Strunk and White apply.

    Python coders from non-English speaking countries: please write

    your comments in English, unless you are 120% sure that the code

    will never be read by people who don't speak your language.

  Block Comments

    Block comments generally apply to some (or all) code that follows them,

Docstring ConventionsWhere stories live. Discover now