类方法和__new__实现的单例模式
# 单例模式 # singleton pattern class Demo: __instance = None @classmethod def singleton(cls): if not cls.__instance: cls.__instance = cls() return cls.__instance class Demo1: __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = object.__new__(cls) return cls.__instance