Item 2: Follow the PEP Style Guide
Notes
- PEP (short for Python Enhancement Proposal) 8 is the style guide for python code
- A common style encourages collaboration
- Let’s you use common tooling
- PEP 8 evolves as the language ecosystem does too
- You should stay up to date with it
- Below highlights some of the main points
Whitespace
- Python whitespace is syntactically significant
- As a result you should
- Use spaces instead of tabs
- Use four spaces for each level of syntactically significant indenting
- Lines should be \(79\) characters or less
- Docstrings and comments should be \(72\) characters or less
- Internal teams may relax the first requirement to \(99\) characters
- Continuation of expressions onto additional lines should be indented by four extra spaces
- In a file
- Functions and classes should be separated by two blank lines
- In a class
- Methods are separated by one line
- In a dictionary,
- No whitespace between key and colon value e.g.
key:valuenotkey : - One space between colon and value if room on the same line, e.g.
: valuenot:value
- No whitespace between key and colon value e.g.
- One whitespace before and after assignment operator
- e.g.
a = bnota=b
- e.g.
- For a type annotation
- No space between variable and colon e.g.
x:notx : - One space before the type information e.g.
x: intnotx:int
- No space between variable and colon e.g.
Naming
Different parts of the language should be named differently
Helps the reader quickly distinguish between variables, constants, functions and classes
- Functions, variables and attributes to use snake_case e.g.
lowercase_underscore - Protected attributes to have a leading underscore, e.g.
_protected_attribute - Private attributes to use a double leading underscore, e.g.
__private_attribute - Classes to use capitalised CamelCase e.g.
ClassName- This includes exceptions
- Instance methods should name the first parameter (the object reference)
self - Class methods should name the first parameter (the class reference)
cls
- Functions, variables and attributes to use snake_case e.g.
Expressions and Statements
- Use inline negation
if a is not binstead of negating a positiveif not a is b - Don’t check for empty containers (e.g.
[]or"") by checking the length- Use
not, e.g.if not some_list- Assume that empty values will evaluate to
False
- Assume that empty values will evaluate to
- Use
- Assume a non-empty container (e.g.
[1]or"a") will evaluateTrue- e.g.
if some_listshould evaluate toTrue
- e.g.
- Avoid single line
if,whileandexceptstatements- Prefer to spread these over multiple lines for readability
- Break a statement over multiple lines by wrapping it in parentheses
- Add line breaks and indentation to make it readable
- Corollary to the above:
- Prefer using parentheses to split over multiple lines
- Avoid using the line continuation marker
\
Imports
- Put
importstatements at the top of a file - Use absolute names for modules
- Don’t use relative names
- e.g. use
from foo import barnotimport footo get the packagebar
- If you must do relative imports, use explicit syntax
- e.g.
from . import foo
- e.g.
- Imports should be:
- Sectioned into the following,
- standard library modules
- third-party modules
- Your own modules
- Each subsection should be sorted alphabetically
- Sectioned into the following,
Automation
- Because there is a common framework of style that means there can be a common tooling ecosystem
- Some popular ones are
- Flake8 is a linter that assesses code style
- Black is a formatter that automatically updates code to conform to the PEP 8 style guide
Black is also officially supported by the Python Software Foundation
$ pip install black $ python -m black example.py reformatted example.py All done! 1 file reformatted
- isort automatically sorts imports
- Ruff combines linting, formatting and import sorting
Plus other features
Can replace the previous three entirely
$ pip install ruff $ ruff check [file] # lint a file $ ruff format [file] # format a file
- Pylint another linting tool
Things to Remember
- Always follow PEP 8 when writing python code
- Sharing a common style helps you collaborate
- Community tooling helps you stick to the PEP 8 style and can be used to catch errors