Python單體模式的幾種常見實(shí)現(xiàn)方法詳解
本文實(shí)例講述了Python單體模式的幾種常見實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
這里python實(shí)現(xiàn)的單體模式,參考了:https://stackoverflow.com/questions/1363839/python-singleton-object-instantiation/1363852#1363852
一、修改父類的 __dict__
class Borg:
_shared_state = {}
def __init__(self):
self.__dict__ = self._shared_state
class Singleton(Borg):
def __init__(self, name):
super().__init__()
self.name = name
def __str__(self):
return self.name
x = Singleton('sausage')
print(x)
y = Singleton('eggs')
print(y)
z = Singleton('spam')
print(z)
print(x)
print(y)
注意,這種方法實(shí)現(xiàn)的并非真正的單體模式?。?/p>
下面幾種方法實(shí)現(xiàn)的才是真正的單體模式
二、使用元類
先看看這里關(guān)于元類的描述:
元類一般用于創(chuàng)建類。
在執(zhí)行類定義時,解釋器必須要知道這個類的正確的元類。解釋器會先尋找類屬性__metaclass__,如果此屬性存在,就將這個屬性賦值給此類作為它的元類。如果此屬性沒有定義,它會向上查找父類中的__metaclass__。如果還沒有發(fā)現(xiàn)__metaclass__屬性,解釋器會檢查名字為__metaclass__的全局變量,如果它存在,就使用它作為元類。否則, 使用內(nèi)置的 type 作為此類的元類。
1. 繼承 type,使用 __call__
注意__call__的參數(shù)
class Singleton(type):
_instance = None
def __call__(self, *args, **kw):
if self._instance is None:
self._instance = super().__call__(*args, **kw)
return self._instance
class MyClass(object):
__metaclass__ = Singleton
print(MyClass())
print(MyClass())
2. 繼承 type,使用 __new__
注意__new__的參數(shù)
class Singleton(type):
_instance = None
def __new__(cls, name, bases, dct):
if cls._instance is None:
cls._instance = super().__new__(cls, name, bases, dct)
return cls._instance
class MyClass(object):
__metaclass__ = Singleton
print(MyClass())
print(MyClass())
3. 繼承 object,使用 __new__
注意__new__的參數(shù)
class Singleton(object):
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
class MyClass(object):
__metaclass__ = Singleton
print(MyClass())
print(MyClass())
下面還有一個很巧妙的方法實(shí)現(xiàn)單體模式
使用類方法classmethod
class Singleton:
_instance = None
@classmethod
def create(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance
def __init__(self):
self.x = 5 # or whatever you want to do
sing = Singleton.create()
print(sing.x) # 5
sec = Singleton.create()
print(sec.x) # 5
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- Python設(shè)計(jì)模式之單例模式實(shí)例
- 常見的在Python中實(shí)現(xiàn)單例模式的三種方法
- Python下singleton模式的實(shí)現(xiàn)方法
- Python設(shè)計(jì)模式之觀察者模式實(shí)例
- python設(shè)計(jì)模式大全
- Python設(shè)計(jì)模式之代理模式實(shí)例
- 5種Python單例模式的實(shí)現(xiàn)方式
- PHP、Python和Javascript的裝飾器模式對比
- python單例模式實(shí)例分析
- 使用簡單工廠模式來進(jìn)行Python的設(shè)計(jì)模式編程
- 舉例講解Python設(shè)計(jì)模式編程中的訪問者與觀察者模式
相關(guān)文章
Jupyter notebook運(yùn)行Spark+Scala教程
這篇文章主要介紹了Jupyter notebook運(yùn)行Spark+Scala教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
對python中的乘法dot和對應(yīng)分量相乘multiply詳解
今天小編就為大家分享一篇對python中的乘法dot和對應(yīng)分量相乘multiply詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
python基礎(chǔ)教程之實(shí)現(xiàn)石頭剪刀布游戲示例
使用PYTHON設(shè)計(jì)一個"石頭,剪子,布"游戲,有時又叫"Rochambeau",下面是實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
python微信公眾號之關(guān)注公眾號自動回復(fù)
這篇文章主要為大家詳細(xì)介紹了python微信公眾號之關(guān)注公眾號自動回復(fù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
python實(shí)現(xiàn)超市管理系統(tǒng)(后臺管理)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超市管理系統(tǒng),增加后臺管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
python sklearn中tsne算法降維結(jié)果不一致問題的解決方法
最近在做一個文本聚類的分析,在對文本數(shù)據(jù)embedding后,想著看下數(shù)據(jù)的分布,于是用sklearn的TSNE算法來降維embedding后的數(shù)據(jù)結(jié)果,當(dāng)在多次執(zhí)行后,竟發(fā)現(xiàn)TSNE的結(jié)果竟然變了,而且每次都不一樣,所以本文就給大家講講如何解決sklearn中tsne算法降維結(jié)果不一致的問題2023-10-10

