PEP8
The official style guide for Python code. Consistency is the key to maintainability.
01. Core Principles
Indentation
Use 4 spaces per indentation level. Avoid tabs.
Line Length
Limit all lines to a maximum of 79 characters.
Naming Conventions
snake_case for functions/variables, CamelCase for classes.
Imports
Imports should be on separate lines and grouped (Standard lib, Third-party, Local).
02. Git-Friendly Standards
Trailing Commas
Always add trailing commas in lists and dictionaries. This ensures that adding a new item only changes one line, keeping diffs clean.
Vertical Formatting
Place function arguments on separate lines. If two developers change different arguments, it won't cause a merge conflict on the same line.
Sorted Imports
Use tools like isort to keep imports alphabetical. This prevents conflicts when multiple files are added.
03. Automated Enforcement
Flake8 & Pylint
Static analysis tools to catch style violations before they merge.
$ pip install flake8
$ flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics