PEP 8 Recommendations
Function Names
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
mixedCase is allowed only in contexts where that's already the prevailing style (for example threading.py
), to retain backwards compatibility.
Function Arguments
If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_
is better than clss
. An even better solution is to avoid such clashes by using a synonym.
Don't use spaces around the =
sign when used to indicate a keyword argument or a default parameter value.
Return statement
Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None
, and an explicit return statement should be present at the end of the function (if reachable).
Indentation
Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent. When using a hanging indent the following should be considered; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.
The 4-space rule is optional for continuation lines.
Blank lines and White Spaces
Avoid extraneous white spaces immediately before the open parenthesis that starts the argument list of a function call:
Surround top-level function and class definitions with two blank lines.
Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).
Use blank lines in functions, sparingly, to indicate logical sections.
Last updated