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__)

浙公网安备 33010602011771号