PY

Python Defaults

Core Python coding conventions

Details

Language / Topic
pythonPython
Category
Style Guide

Rules

balanced
- Follow PEP 8 style: 4-space indentation, snake_case for functions and variables, PascalCase for classes.
- Use type hints for all function signatures. Run `mypy` or `pyright` for static type checking.
- Use list comprehensions over `map()`/`filter()` for simple transformations — they're more readable and Pythonic.
- Use context managers (`with open() as f:`) for resource management — they guarantee cleanup even on exceptions.
- Use f-strings for string formatting. Use `pathlib.Path` over `os.path` for file operations.
- Prefer list/dict/set comprehensions over manual loops for simple transformations.
- Use `dataclasses` or `pydantic.BaseModel` for structured data instead of plain dicts.
- Use context managers (`with` statements) for resource management (files, connections, locks).
- Raise specific exceptions (`ValueError`, `TypeError`, custom exceptions) — never bare `raise Exception`.