1.用 __new__方法 class Singleton(object): def __new__(cls): if not hasattr(cls,'_instance'): cls._instance=super(Singleton,cls).__new__(cls) # cls.__inst ...
1.用 __new__
方法
class Singleton(object):
def __new__(cls):
if not hasattr(cls,'_instance'):
cls._instance=super(Singleton,cls).__new__(cls)
# cls.__instance = object.__new__(cls) #這樣也可以
return cls._instance
a = Singleton()
b = Singleton()
c = Singleton()
print(a,id(a))
print(b,id(b))
print(c,id(c))
輸出:
<__main__.Singleton object at 0x000000000287A240> 42443328
<__main__.Singleton object at 0x000000000287A240> 42443328
<__main__.Singleton object at 0x000000000287A240> 42443328
2.共用屬性方法
共用屬性:創建實例時把所有實例的__dict__
指向同一個字典,這樣它們具有相同的屬性和方法.
class Borg(object):
_state = {}
def __new__(cls, *args, **kw):
ob = super(Borg, cls).__new__(cls, *args, **kw)
ob.__dict__ = cls._state
return ob
class MyClass2(Borg):
a = 1
b1 = MyClass2()
b2 = MyClass2()
b3 = MyClass2()
b1.a='薩菲的'
b2.a='薩菲02'
b1.a='薩菲03'
MyClass2.a = 333
print(id(b1),id(b2),id(b3))
print(b1.a,b3.a,b2.a)
print(id(b1.a),id(b2.a),id(b3.a))
輸出:
42902024 42902080 42901688
薩菲03 薩菲03 薩菲03
167568080 167568080 167568080
裝飾器版本
def singleton(cls, *args, **kw):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls(*args, **kw)
return instances[cls]
return getinstance
@singleton
class MyClass:
a = 1
b1 = MyClass()
b2 = MyClass()
b3 = MyClass()
print(id(b1),id(b2),id(b3))
print(b1.a,b3.a,b2.a)
輸出:
42712928 42712928 42712928
1 1 1
類方法版
class Foo():
__v = None
@classmethod
def get_instance(cls):
if cls.__v:
print("true: 這是cls.__v",cls.__v)
return cls.__v
else:
print("false: 這是cls.__v", cls.__v)
cls.__v = Foo()
print("false_1: 這是cls.__v", cls.__v)
return cls.__v
obj = Foo.get_instance()
obj1 = Foo.get_instance()
obj2 = Foo.get_instance()
obj3 = Foo.get_instance()
print(obj,'哈哈',id(obj))
print(obj1,'哈哈',id(obj1))
print(obj2,'哈哈',id(obj2))
print(obj3,'哈哈',id(obj3))
輸出:
false: 這是cls.__v None
false_1: 這是cls.__v <__main__.Foo object at 0x00000000025002E8>
true: 這是cls.__v <__main__.Foo object at 0x00000000025002E8>
true: 這是cls.__v <__main__.Foo object at 0x00000000025002E8>
true: 這是cls.__v <__main__.Foo object at 0x00000000025002E8>
<__main__.Foo object at 0x00000000025002E8> 哈哈 38798056
<__main__.Foo object at 0x00000000025002E8> 哈哈 38798056
<__main__.Foo object at 0x00000000025002E8> 哈哈 38798056
<__main__.Foo object at 0x00000000025002E8> 哈哈 38798056
單例預設:
'''
學習中遇到問題沒人解答?小編創建了一個Python學習交流群:711312441
尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書!
'''
class Singleton(object):
def __new__(cls,*args,**kwargs):
if not hasattr(cls,'_instance'):
print('你好啊',)
cls._instance = super(Singleton,cls).__new__(cls)
print(cls._instance, '阿斯蒂芬01')
return cls._instance
print( '阿斯蒂芬')
print(cls._instance, '阿斯蒂芬02')
return cls._instance
a = Singleton()
print('這是a',id(a),a)
b = Singleton()
print('這是b',id(b),b)
輸出:
你好啊
<__main__.Singleton object at 0x000000000288BEF0> 阿斯蒂芬01
這是a 42516208 <__main__.Singleton object at 0x000000000288BEF0>
阿斯蒂芬
<__main__.Singleton object at 0x000000000288BEF0> 阿斯蒂芬02
這是b 42516208 <__main__.Singleton object at 0x000000000288BEF0>