Python: Singleton

 

 

class Singleton:
    __obj = None
    __initiated = False

    def __new__(cls, *args, **kwargs):
        if cls.__obj is None:
            cls.__obj = super(Singleton, cls).__new__(cls)
        return cls.__obj

    def __init__(self, name):
        if not self.__initiated:
            self.name = name


a = Singleton('aaa')
b = Singleton('bbb')
print(a, b)
print(a.__dict__, b.__dict__)

 

posted @ 2022-09-15 10:45  ascertain  阅读(13)  评论(0编辑  收藏  举报