Python Best Practices

Chapter 2 · Practice 1 of 5

Follow PEP 8 and let a formatter apply it

From Style, formatting and linting

Why it matters

PEP 8 is the shared style guide for Python. The parts that matter most day to day:

ThingConvention
Indentation4 spaces, no tabs
Functions, variables, modulessnake_case
ClassesCapWords
ConstantsUPPER_CASE at module level
Line length79 by default; a team may agree on up to 99

PEP 8 itself says consistency within a project matters more than the guide, and consistency within a module matters most.

Do not enforce any of this in code review. Run a formatter on save and in CI, so style never appears in a review comment again. Ruff formats with Black-compatible output and lints in the same tool.

Example

TOML
# pyproject.toml
[tool.ruff]
line-length = 99
target-version = "py314"

[tool.ruff.lint]
select = ["E", "F", "I", "B", "UP", "SIM"]