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))

 

posted @ 2018-01-10 04:13  许小伍  阅读(128)  评论(0编辑  收藏  举报