Singleton in Python

The Singleton pattern enforces having one instance of a class. This tutorial will showcase how to implement the singleton pattern in Python.

Implementing the Singleton pattern

Using the __new__ dunder method, the creation of the object can be controlled since __new__ is called before the __init__ method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Singleton:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(Singleton, cls).__new__(cls)
        return cls._instance

s1 = Singleton()
s2 = Singleton()
print(s1 == s2) # outputs True

s1 and s2 are the same object in memory.

Why is this beneficial?

The singleton pattern is useful for certain cases:

  1. Logger
  2. Thread Pooling
  3. Caching
  4. Configuration