设计模式
单例模式
class MySingleton: __obj = None __init_flag = True def __new__(cls, *args, **kwargs): if not cls.__obj: cls.__obj = super().__new__(cls) return cls.__obj def __init__(self, name): if MySingleton.__init_flag: print("Init ......") MySingleton.__init_flag = False self.name = name s = MySingleton("nxr") print(s.name) s1 = MySingleton("jsm") # print(s.name) print(s1.name)