Python Design Patterns Guide
Brandon Rhodes's free guide to design patterns in Python, which is honest about which Gang of Four patterns Python makes unnecessary and which still matter. Its essay on composition over inheritance is one of the clearest explanations of Adapter, Bridge and Decorator written in any language.
Guide
Other
Brandon Rhodes
A few evenings for the whole guide
Intermediate
Free
What it is
The Python Design Patterns Guide is a free website by Brandon Rhodes, a long-time Python speaker and author. He describes it as an evolving guide that makes design patterns in Python easier to find and read than the conference talks where much of this knowledge used to live.
The guide is valuable precisely because it does not treat the Gang of Four book as a checklist. For each pattern it asks whether Python's own features, such as first-class functions, modules and duck typing, already solve the problem, and shows the Pythonic version when they do.
What it covers
The guide is organised into four parts:
- Gang of Four principles: The Composition Over Inheritance Principle, which walks through one logging problem solved with Adapter, Bridge and Decorator, and compares them with subclassing and other shortcuts.
- Python-specific patterns: the Global Object pattern, the Prebound Method pattern and the Sentinel Object pattern.
- Gang of Four creational patterns: Abstract Factory, Builder, Factory Method, Prototype and Singleton.
- Gang of Four structural and behavioural patterns: Composite, Decorator, Flyweight and Iterator.
It does not cover all 23 patterns, and it says so by only listing what exists. Use it alongside a complete catalogue for the rest.
Why it is worth reading
Many pattern tutorials port Java code line by line into Python, which produces awkward code full of classes that should be functions. This guide does the opposite: it starts with the problem, shows how the classic pattern solves it and then shows when a module, a function or a simple object is the better answer. That judgement, knowing when not to use a pattern, is the hardest part of learning them.
The composition over inheritance essay alone is worth the visit. Seeing Adapter, Bridge and Decorator applied to the same problem makes the difference between these look-alike patterns obvious.
How long it takes
Each page is a single essay. You can read the whole guide over a few evenings, then return to individual pages when you meet the pattern in real code.
Prerequisites
Comfort with Python classes, functions and modules. Familiarity with the idea of design patterns helps but is not required.
Common questions
Is it free? Yes, the whole site is free to read.
Is it only useful for Python developers? Mostly, but the composition over inheritance essay is useful in any object-oriented language.
Best for: Python developers who want to learn design patterns without writing Java in Python.
Ready to start?
Opens on Brandon Rhodes 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.