A collection of design patterns and idioms in Python.
Remember that each pattern has its own trade-offs. And you need to pay attention more to why you’re choosing a certain pattern than to how to implement it.
When an implementation is added or modified, please review the following guidelines:
Docstrings
Add module level description in form of a docstring with links to corresponding references or other useful information.
Add “Examples in Python ecosystem” section if you know some. It shows how patterns could be applied to real-world problems.
facade.py has a good example of detailed description,
but sometimes the shorter one as in template.py would suffice.
Python 2 compatibility
To see Python 2 compatible versions of some patterns please check-out the legacy tag.
Update README
When everything else is done - update corresponding part of README.
Travis CI
Please run the following before submitting a patch
black . This lints your code.
Then either:
tox or tox -e ci37 This runs unit tests. see tox.ini for further details.
If you have a bash compatible shell use ./lint.sh This script will lint and test your code. This script mirrors the CI pipeline actions.
You can also run flake8 or pytest commands manually. Examples can be found in tox.ini.
Contributing via issue triage
You can triage issues and pull requests which may include reproducing bug reports or asking for vital information, such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is to subscribe to python-patterns on CodeTriage.
🚫 Anti-Patterns
This section lists some common design patterns that are not recommended in Python and explains why.
🧱 Singleton
Why not:
Python modules are already singletons — every module is imported only once.
python-patterns
A collection of design patterns and idioms in Python.
Remember that each pattern has its own trade-offs. And you need to pay attention more to why you’re choosing a certain pattern than to how to implement it.
Current Patterns
Creational Patterns:
Structural Patterns:
Behavioral Patterns:
Design for Testability Patterns:
Fundamental Patterns:
Others:
Videos
Design Patterns in Python by Peter Ullrich
Sebastian Buczyński - Why you don’t need design patterns in Python?
You Don’t Need That!
Pluggable Libs Through Design Patterns
Contributing
When an implementation is added or modified, please review the following guidelines:
Docstrings
Add module level description in form of a docstring with links to corresponding references or other useful information.
Add “Examples in Python ecosystem” section if you know some. It shows how patterns could be applied to real-world problems.
facade.py has a good example of detailed description, but sometimes the shorter one as in template.py would suffice.
Python 2 compatibility
To see Python 2 compatible versions of some patterns please check-out the legacy tag.
Update README
When everything else is done - update corresponding part of README.
Travis CI
Please run the following before submitting a patch
black .This lints your code.Then either:
toxortox -e ci37This runs unit tests. see tox.ini for further details../lint.shThis script will lint and test your code. This script mirrors the CI pipeline actions.You can also run
flake8orpytestcommands manually. Examples can be found intox.ini.Contributing via issue triage
You can triage issues and pull requests which may include reproducing bug reports or asking for vital information, such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is to subscribe to python-patterns on CodeTriage.
🚫 Anti-Patterns
This section lists some common design patterns that are not recommended in Python and explains why.
🧱 Singleton
Why not:
🌀 God Object
Why not:
🔁 Inheritance overuse
Why not: