day_5.5 单例

 1 2018-5-5 15:00:25
 2 单例 : 就是对象只有一个
 3 '''
 4 
 5 class main(object):
 6 
 7     __instance = None
 8     def __new__(cls,):
 9         if cls.__instance ==None:
10             cls.__instance = object.__new__(cls)
11             return cls.__instance
12         else:
13             # return 上一次创建的对象的引用
14             return cls.__instance
15 
16 a = main()
17 print(id(a))
18 b= main()
19 print(id(b))
原文地址:https://www.cnblogs.com/zhen1996/p/8997227.html