how to create singleton class with arguments in python - Stack Overflow

PHOTO EMBED

Sun May 09 2021 08:22:43 GMT+0000 (Coordinated Universal Time)

Saved by @arielvol #python

class Singleton (type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

# Python 2
class MyClass():
    __metaclass__= Singleton

# Python 3
class MyClass(metaclass=Singleton):
     pass
content_copyCOPY

https://stackoverflow.com/questions/51896862/how-to-create-singleton-class-with-arguments-in-python