本文参考自: 原文地址
class single_instance(object):__instance=None
def __init__(self):
pass
def __new__(cls, *args, **kwargs):
if cls.__instance==None:
cls.__instance=object.__new__(cls,*args,**kwargs)
return cls.__instance
a=single_instance()
b=single_instance()
print(a)
print(b)
输出结果如下:
<__main__.singleton object at 0x0000016D5191D320>
<__main__.singleton object at 0x0000016D5191D320>