Introduction
Python Enhancement Proposal #8, otherwise known as PEP 8, is the style guide for how to format Python code. You are welcome to write Python code any way you want, as long as it has valid syntax. However, using a consistent style makes your code more approachable and easier to read. Sharing a common style with other Python programmers in the larger community facilitates collaboration on projects. But even if you are the only one who will ever read your code, following the style guide will make it easier for you to change things later, and can help you avoid many common errors.
PEP 8 provides a wealth of details about how to write clear Python code. It continues to be updated as the Python language evolves. It’s worth reading the whole guide online (https://www.python.org/dev/peps/pep-0008/). Here are a few rules you should be sure to follow.
Whitespace
- Use spaces instead of tabs for indentation.
- Use four spaces for each level of syntactically significant indenting.
- Lines should be 79 characters in length or less.
- Continuations of long expressions onto additional lines should be indented by four extra spaces from their normal indentation level.
- In a file, functions and classes should be separated by two blank lines.
- In a class, methods should be separated by one blank line.
- In a dictionary, put no whitespace between each key and colon, and put a single space before the corresponding value if it fits on the same line.
- Put one—and only one—space before and after the
=
operator in a variable assignment. - For type annotations, ensure that there is no separation between the variable name and the colon, and use a space before the type information.
Naming functions
- Functions, variables, and attributes should be in
lowercase_underscore
format. - Protected instance attributes should be in
_leading_underscore
format. - Private instance attributes should be in
__double_leading_underscore
format. - Classes (including exceptions) should be in
CapitalizedWord
format. - Module-level constants should be in
ALL_CAPS
format. - Instance methods in classes should use
self
, which refers to the object, as the name of the first parameter. - Class methods should use
cls
, which refers to the class, as the name of the first parameter.
Expressions and Statements
- Use inline negation (
if a is not b
) instead of negation of positive expressions (if not a is b
). - Don’t check for empty containers or sequences (like
[]
or''
) by comparing the length to zero (if len(somelist) == 0
). Useif not somelist
and assume that empty values will implicitly evaluate toFalse
. - The same thing goes for non-empty containers or sequences (like
[1]
or'hi'
). The statementif somelist
is implicitlyTrue
for non-empty values. - Avoid single-line
if
statements,for
andwhile
loops, andexcept
compound statements. Spread these over multiple lines for clarity. - If you can’t fit an expression on one line, surround it with parentheses and add line breaks and indentation to make it easier to read.
- Prefer surrounding multiline expressions with parentheses over using the
\
line continuation character.
Imports
- Always put
import
statements (includingfrom x import y
) at the top of a file. - Always use absolute names for modules when importing them, not names relative to the current module’s own path. For example, to import the
foo
module from within thebar
package, you should usefrom bar import foo
, not justimport foo
. - If you must do relative imports, use the explicit syntax
from . import foo
. - Imports should be in sections in the following order: standard library modules, third-party modules, your own modules. Each subsection should have imports in alphabetical order.