Pep 8
Pep 8
Python programmers
are especially sensitive to the effects of whitespace on code clarity.
• Don’t put spaces around list indexes, function calls, or keyword argument
assignments.
Naming: PEP 8 suggests unique styles of naming for different parts in the language.
This makes it easy to distinguish which type corresponds to each name when reading
code.
• Instance methods in classes should use self as the name of the first parameter
(which refers to the object).
• Class methods should use cls as the name of the first parameter (which refers to
the class).
Expressions and Statements: The Zen of Python states: “There should be one—and
preferably only one—obvious way to do it.” PEP 8 attempts to codify this style in
its guidance for expressions and statements.
• Don’t check for empty values (like [] or '') by checking the length (if
len(somelist) == 0). Use if not somelist and assume empty values implicitly
evaluate to False.
• The same thing goes for non-empty values (like [1] or 'hi'). The statement if
somelist is implicitly True for non-empty values.
• Avoid single-line if statements, for and while loops, and except compound
statements. Spread these over multiple lines for clarity.
• If you must do relative imports, use the explicit syntax from . import foo.