单例设计模式

 1 class Singleton(object):
 2 
 3     def __new__(cls, *args, **kwargs):
 4         if not hasattr(cls, '_the_instance'):
 5             cls._the_instance = object.__new__(cls, *args, **kwargs)
 6         return cls._the_instance
 7 
 8 
 9 class A(Singleton):
10 
11     def __init__(self):
12         print('i am __init__')
13 
14 a = A()
15 b = A()
16 print(id(a))
17 print(id(b))
1 i am __init__
2 i am __init__
3 43557776
4 43557776

 

posted @ 2018-01-15 14:31  魂~  阅读(162)  评论(0编辑  收藏  举报