标签:hacks pre 参考 singleton 实现 style fun class single
用__new__实现单例模式
class Singleton(object): _instance=None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance=super(Singleton,cls).__new__(cls,*args,**kwargs) return cls._instance class Myclass(Singleton): a=1 one=Myclass() two=Myclass() print(one==two) print(one is two) print(id(one),id(two)) #True #True #1268707849048 1268707849048
参考
http://funhacks.net/2017/01/17/singleton/
标签:hacks pre 参考 singleton 实现 style fun class single
原文地址:https://www.cnblogs.com/z-x-y/p/10068512.html