Python Best Practices
A practical guide to Python best practices for people who write applications, services and scripts that other people depend on. It covers supported versions and project setup with pyproject.toml and a lockfile, style with PEP 8 and Ruff, type hints and data modelling, error handling, logging, concurrency and performance, testing with pytest, and the security mistakes Python makes easy.
8
Sefism · Oct 2026
37
Sefism · Oct 2026
8
Sefism · Oct 2026
Stacks and tools
Sefism · Oct 2026
Best practices
Web Development
Sefism
22 min read
Intermediate
Free
What this best practices guide is
A practical guide to Python best practices for people who write applications, services and scripts that other people depend on. It covers supported versions and project setup with pyproject.toml and a lockfile, style with PEP 8 and Ruff, type hints and data modelling, error handling, logging, concurrency and performance, testing with pytest, and the security mistakes Python makes easy.
It has 8 chapters and 37 practices, each with why it matters, what to do and what to avoid. For Python developers and CS students.
Chapter by chapter
1. Versions, environments and project setup (5 practices)
The decisions that every Python project makes on day one: which version to run, how to isolate dependencies and how to describe the project so anyone can rebuild it.
2. Style, formatting and linting (5 practices)
How to make Python code look the same across a team without arguing about it, and the idioms that make it easier to read.
3. Type hints and data modelling (4 practices)
How to use type hints and a type checker to catch bugs before runtime, and how to model data so it is not a dictionary of unknown shape.
4. Errors and resource handling (5 practices)
How to raise, catch and report exceptions so failures are visible and specific, and how to make sure files, connections and locks are always released.
5. Logging and configuration (4 practices)
How to log so problems can be diagnosed in production, and how to load settings so the same code runs in every environment.
6. Concurrency and performance (5 practices)
Choosing between asyncio, threads and processes, keeping async code from blocking, and measuring before you optimise.
7. Testing with pytest (4 practices)
How to set up pytest so tests are fast, isolated and run against the code users install.
8. Security pitfalls in Python (5 practices)
The standard library and common packages make a few dangerous things very easy. These practices close the most common holes.
How it works on Sefism
The guide reads like a short course: a syllabus, then one chapter per page in a focused reader, with a quiz at the end of each chapter for members. The opening chapters are open to everyone. Adding it to your account uses credits, opens every chapter and keeps your progress.
Every claim is checked against the sources listed on the page (8 cited).
Ready to start?
Opens on Sefism in a new tab.
Stuck on something specific?
Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.
Follow along
New writing, resources and project ideas land here first.