How to Implement the Singleton Design Pattern in Python with Examples

When designing an application, certain situations demand that a class should have only one instance. The Singleton design pattern addresses this need by ensuring that a class has only one instance and provides a global point of access to that instance. In this article, we will delve into how to implement the Singleton pattern in Python with detailed examples.

1. Understanding the Singleton Design Pattern.

  1. The Singleton pattern restricts the instantiation of a class to a single object.
  2. This is particularly useful when we want to control access to some shared resource, such as a database connection or a file.
  3. Implementing the Singleton pattern involves creating a class that allows only one instance to be created and providing a way to access that instance.

2. Implementing the Singleton Pattern in Python.

  1. Here, we present a few methods to implement the Singleton pattern in Python:

2.1 Method 1: Use a Class Variable to Save the Instance.

  1. One of the simplest ways to implement a Singleton in Python is by using a class variable to save a single instance.
  2. You should also implement a class method ( decorated by the @classmethod ), in this method, it will process and return the singleton object of the class.
  3. Below is the example source code.
    class Singleton:
        instance_obj = None
        def __init__(self):
            # initialization code
            print('instance is created.')
        def get_singleton(cls):
            if cls.instance_obj == None:
                cls.instance_obj = Singleton()
            return cls.instance_obj    
    def test_singleton_method1():
        singleton_instance = Singleton.get_singleton()
        singleton_instance1 = Singleton.get_singleton()
    if __name__ == "__main__":
  4. When you run the above example Python source code, you will get the below output.
    instance is created.
  5. From the above example code execution output, we can see the class Singleton‘s __init__ method is called only once when you call the get_singleton() method for the first time.
  6. The 2 Python object’s id is the same, which means they refer to the same object ( singleton instance of the class ).

2.2 Method 2: Using a Decorator.

  1. We can also use a decorator to ensure that only one instance of a class is created.
    def singleton(cls):
        instances = {}
        def get_instance(*args, **kwargs):
            if cls not in instances:
                instances[cls] = cls(*args, **kwargs)
            return instances[cls]
        return get_instance
    class SingletonClass:
        def __init__(self):
            print('SingletonClass __init__ is called.')
    def test_singleton_method2():
        obj1 = SingletonClass()
        obj2 = SingletonClass()
    if __name__ == "__main__":
  2. When you run the above example source code, it will generate the below output.
    SingletonClass __init__ is called.

2.3 Method 3: Using a Metaclass.

  1. Using a metaclass allows us to control the instantiation of the class and enforce the Singleton pattern.
    class SingletonMeta(type):
        _instances = {}
        def __call__(cls, *args, **kwargs):
            if cls not in cls._instances:
                cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
            return cls._instances[cls]
    class SingletonClass1(metaclass=SingletonMeta):
        def __init__(self):
            # initialization code
            print('SingletonClass1 __init__ is called.')
    def test_singleton_method3():
        obj1 = SingletonClass1()
        obj2 = SingletonClass1()
    if __name__ == "__main__":
  2. When you run the above example source code, it will generate the below output.
    SingletonClass1 __init__ is called.

3. Conclusion.

  1. Implementing the Singleton pattern in Python ensures that only one instance of a class exists throughout the application’s lifecycle.
  2. We explored various methods, including using a class method and variable, decorators, and metaclasses, to achieve this.
  3. Understanding when and how to apply the Singleton pattern is crucial for ensuring the efficient management of shared resources and maintaining control over class instantiation.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.