PEP 8
PEP = Python Enhancement Proposal
PEP 8 is Style Guide for Python Code
The code is read much more often than it is written.
We'll cover these main sections:
Naming conventions
Blank Lines
Whitespaces
Indentation
Naming conventions
Constants:
UPPERCASE_WITH_UNDERSCORES
Variables – no sctrict rules except:
normal_variable
_private_variable
avoiding_conflict_
__magic_method__
__name_mangling
Functions:
lowercase_with_underscores
Modules:
shortalllowercases
long_can_use_underscores_for_readability
Class:
CapWords
Exceptions:
CapWordsError / CapWordsException
Blank lines
Surround top-level function and class definitions with two blank lines.
Method definitions inside a class are surrounded by a single blank line.
Extra blank lines may be used (sparingly) to separate groups of related functions.
Blank lines may be omitted between a bunch of related oneliners (e.g. a set of dummy implementations).
Use blank lines in functions, sparingly, to indicate logical sections.
Imports
Imports should usually be on separate lines
Yes:
No:
Imports should be grouped in the following order:
standard library imports
related third party imports
local application/library specific imports
You should put a blank line between each group of imports.
Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path )
But in some cases - when dealing with complex package layouts where using absolute imports would be unnecessarily verbose:
Whitespaces
Avoid extraneous whitespace in the following situations:
Immediately after 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: dct['key'] = lst[index]
No: dct ['key'] = lst [index]
Don't use spaces around the = sign when used to indicate a keyword argument or a default parameter value.
Yes:
No:
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
These examples are good:
NO:
Using if:
Maximum line length
Limit all lines to a maximum of 79 characters.
80 lines vs 120 line = Eternal Holy War
For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.
For PyCharm (may be non-actual):
File >> Settings >> Editor >> Code Style: Right margin (columns)
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better.
Also:
Last updated