Robots Atlas>ROBOTS ATLAS

Python — From Basics to Advanced · Design Patterns in Python

Singleton, Borg, and module as singleton

Design Patterns in Python

Introduction

Singleton — the most-debated creational pattern. In Python it has several faces: a classic __new__, Alex Martelli's "Monostate" Borg, a metaclass, a decorator. But the most idiomatic answer is usually: a module. This lesson shows when Singleton makes sense, when it is an anti-pattern, how the stdlib solves the same problem (logging, configparser), and why dependency injection is often the better choice.