← source index

singleton.py

9 lines  ·  260 B  ·  python
class Singleton(type):
    obj_instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls.obj_instances:
            cls.obj_instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)

        return cls.obj_instances[cls]