
Python Design Patterns
Python Design Patterns¶ Welcome! I’m Brandon Rhodes (website, Twitter) and this is my evolving guide to design patterns in the Python programming language. This site is letting me collect my ideas about Python and Design Patterns all in one place.
The Decorator Pattern - python-patterns.guide
The design pattern became famous as the “Decorator Pattern” with the 1994 publication of the Gang of Four’s Design Patterns book. In 2003, the Python core developers decided to re-use the term decorator for a completely unrelated feature they were adding to Python 2.4.
The Factory Method Pattern - python-patterns.guide
The “Factory Method” pattern is a poor fit for Python. It was designed for underpowered programming languages where classes and functions can’t be passed as parameters or stored as attributes. In those languages, the Factory Method serves …
The Composition Over Inheritance Principle - python-patterns.guide
Let’s take a single design problem and watch how this principle works itself out through several of the classic Gang of Four design patterns. Each design pattern will assemble simple classes, unburdened by inheritance, into an elegant runtime solution.
Gang of Four book - python-patterns.guide
At the expense of more code, this practice moved programmers one step towards the freedom we enjoy in a dynamic language like Python. Each of the book’s further chapters tackled a different design conundrum posed by the era’s limited programming languages and solved it.
The Singleton Pattern - python-patterns.guide
Python was already using the term singleton before the “Singleton Pattern” was defined by the object oriented design pattern community. So we should start by distinguishing the several meanings of “singleton” in Python.
The Iterator Pattern - python-patterns.guide
Python’s decision to re-use its for statement syntax inside of expressions — instead of going to the trouble of inventing a separate special-purpose syntax for inline iteration — makes the language simpler, easier to learn, and easier to remember. The pattern: the …
Refactoring by Martin Fowler - python-patterns.guide
Martin Fowler’s Refactoring: Improving the Design of Existing Code is more heavy focused on object oriented programming than will typically be useful for a Python codebase, but it is still valuable for its practical approach to code. Instead of comparing grand high-level architectures, its habit is to explain a pattern by starting down in the ...
The Builder Pattern - python-patterns.guide
This article will start by describing the Builder pattern’s most popular use in Python programs. Next we will glance at what the Gang of Four thought the pattern’s primary purpose was going to be, and explore why it is rarely used that way in Python.
The Flyweight Pattern - python-patterns.guide
Flyweight objects are such a perfect fit for Python that the language itself uses the pattern for such popular values as identifiers, some integers, and Boolean true and false. A perfect example of the Flyweight Pattern is the Python intern() function.