Python 单例模式

 1 class Dog(object):
 2 
 3     __instance = None   # 设置一个变量来判断储存构造出来得例子
 4 
 5     def __new__(cls):
 6         if cls.__instance == None:
 7             cls.__instance = object.__new__(cls)
 8             return cls.__instance
 9         else:
10             # return 上一次创建得对象得引用
11             return cls.__instance
12 
13 a = Dog()
14 print(id(a))
15 b = Dog()
16 print(id(b))
原文地址:https://www.cnblogs.com/Hunter-541695/p/9333065.html