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.
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.
Functions, variables, and attributes should be in
Protected instance attributes should be in
Private instance attributes should be in
Classes (including exceptions) should be in
Module-level constants should be in
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
'') by comparing the length to zero (
if len(somelist) == 0). Use
if not somelistand assume that empty values will implicitly evaluate to
The same thing goes for non-empty containers or sequences (like
'hi'). The statement
if somelistis implicitly
Truefor non-empty values.
exceptcompound 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.
from 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
foomodule from within the
barpackage, you should use
from bar import foo, not just
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.