1 class Single: 2 instance = None 3 4 def __new__(cls, *args, **kwargs): 5 if cls.instance: 6 return cls.instance 7 cls.instance = object.__new__(cls) 8 return cls.instance 9 10 o1 = Single() 11 o2 = Single() 12 print(o1) # <__main__.Single object at 0x00000000021EDAC8> 13 print(o2) # <__main__.Single object at 0x00000000021EDAC8>
原文:https://www.cnblogs.com/zze46/p/9896600.html