python单例设计模式

 1 class Dog(object):
 2     __instance = None
 3 
 4     def __init__(self):
 5         pass
 6 
 7     def __new__(cls):
 8         if not cls.__instance:
 9             cls.__instance = object.__new__(cls)
10         return cls.__instance
11 
12 d = Dog()
13 dd = Dog()
14 #d和dd的id相同
15 print(id(d))
16 print(id(dd))
原文地址:https://www.cnblogs.com/xhcdream/p/8255992.html