Python 并发编程(上) (5)

简单的实现单例模式

class Singleton: instance = None def __init__(self, name): self.name = name def __new__(cls, *args, **kwargs): # 返回空对象 if cls.instance: return cls.instance cls.instance = object.__new__(cls) return cls.instance obj1 = Singleton(\'alex\') obj2 = Singleton(\'SB\') print(obj1,obj2)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgzgfw.html