Python下簡易的單例模式詳解
Python 下的單例模式
要點(diǎn):
- 1.某個類只能有一個實(shí)例;
- 2.它必須自行創(chuàng)建這個實(shí)例;
- 3.它必須自行向整個系統(tǒng)提供這個實(shí)例
方法:重寫new函數(shù)
應(yīng)該考慮的情況:
- 1.這個單例的類可能繼承了別的類
- 2.這個單例的類還有可能要接收參數(shù)來實(shí)例化
要點(diǎn):
實(shí)例化的過程其實(shí)不是直接調(diào)用init的,首先是new分配一塊空間來創(chuàng)建實(shí)例,再由init對這個實(shí)例進(jìn)行初始化.我們無法阻止new和init的調(diào)用,我們只能是限制他們的內(nèi)容,以此使他們能達(dá)到單例的目的
代碼:
class people(object):
def __new__(cls,*args,**kargs):
return super(people,cls).__new__(cls)
def __init__(self,name):
self.name = name
def talk(self):
print("hello,I am %s" %self.name)
class student(people):
def __new__(cls,*args,**kargs):
if not hasattr(cls,"instance"):
cls.instance = super(student,cls).__new__(cls,*args,**kargs)
return cls.instance
a = student("Timo")
print(a)
b = student("kysa")
c = student("Luyi")
a.talk()
b.talk()
print(c)
這里的輸出結(jié)果是:
<__main__.student object at 0x0000025AC48BF2E8>
hello,I am Luyi
hello,I am Luyi
<__main__.student object at 0x0000025AC48BF2E8>
可以確定的是: 確實(shí)是單例了,因?yàn)閍的id和b,c的id是一致的
但是為什么:a先創(chuàng)建明明是Timo,可是為什么a的name變成了Luyi呢?
原因:
雖然確實(shí)是a這個實(shí)例,但是在最后c重新調(diào)用了new,返回了a的實(shí)例,再經(jīng)過init,改變了a的屬性,執(zhí)行時name ->Luyi.
解決:
這種情況下,我們只需要設(shè)置類變量,讓init在類變量的限制下,只對類進(jìn)行一次有效的初始化.
代碼:
class people(object):
def __new__(cls,*args,**kargs):
return super(people,cls).__new__(cls)
def __init__(self,name):
self.name = name
def talk(self):
print("hello,I am %s" %self.name)
class student(people):
def __new__(cls,*args,**kargs):
if not hasattr(cls,"instance"):
cls.instance = super(student,cls).__new__(cls,*args,**kargs)
return cls.instance
def __init__(self,name):
if not hasattr(self,"init_fir"):
self.init_fir = True
super(student,self).__init__(name)
a = student("Timo")
print(a)
b = student("kysa")
c = student("Luyi")
a.talk()
b.talk()
print(c)
好了,到這里就用Python實(shí)現(xiàn)了一個簡易的單例模式.
以上所述是小編給大家介紹的Python下簡易的單例模式詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- python 實(shí)現(xiàn)單例模式的5種方法
- Python單例模式的四種創(chuàng)建方式實(shí)例解析
- python單例模式原理與創(chuàng)建方法實(shí)例分析
- python單例模式的多種實(shí)現(xiàn)方法
- 簡單了解python單例模式的幾種寫法
- 聊聊python里如何用Borg pattern實(shí)現(xiàn)的單例模式
- Python中實(shí)現(xiàn)單例模式的n種方式和原理
- python單例模式獲取IP代理的方法詳解
- Python中單例模式總結(jié)
- Python單例模式實(shí)例詳解
- python 6種方法實(shí)現(xiàn)單例模式
相關(guān)文章
Python實(shí)現(xiàn)的微信紅包提醒功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的微信紅包提醒功能,結(jié)合實(shí)例形式分析了Python使用微信模塊itchat實(shí)現(xiàn)微信紅包提醒操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08
wxPython繪圖模塊wxPyPlot實(shí)現(xiàn)數(shù)據(jù)可視化
這篇文章主要為大家詳細(xì)介紹了wxPython繪圖模塊wxPyPlot實(shí)現(xiàn)數(shù)據(jù)可視化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
python3 動態(tài)模塊導(dǎo)入與全局變量使用實(shí)例
今天小編就為大家分享一篇python3 動態(tài)模塊導(dǎo)入與全局變量使用實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
python讀取mat文件生成h5文件的實(shí)現(xiàn)
這篇文章主要介紹了python讀取mat文件生成h5文件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Python使用pylab庫實(shí)現(xiàn)繪制直方圖功能示例
這篇文章主要介紹了Python使用pylab庫實(shí)現(xiàn)繪制直方圖功能,結(jié)合實(shí)例形式分析了Python數(shù)據(jù)讀取、遍歷以及基于pylab庫繪制直方圖的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06

