這篇文章主要介紹Python中屬性和內(nèi)建屬性的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)成立于2013年,先為石城等服務(wù)建站,石城等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為石城企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。1. 私有屬性添加getter和setter方法
class Money(object): def __init__(self): self.__money = 0 def getMoney(self): return self.__money def setMoney(self, value): if isinstance(value, int): self.__money = value else: print("error:不是整型數(shù)字")
2. 使用property升級(jí)getter和setter方法
class Money(object): def __init__(self): self.__money = 0 def getMoney(self): return self.__money def setMoney(self, value): if isinstance(value, int): self.__money = value else: print("error:不是整型數(shù)字") money = property(getMoney, setMoney)
運(yùn)行結(jié)果:
In [1]: from get_set import Money In [2]: In [2]: a = Money() In [3]: In [3]: a.money Out[3]: 0 In [4]: a.money = 100 In [5]: a.money Out[5]: 100 In [6]: a.getMoney() Out[6]: 100
3. 使用property取代getter和setter方法
@property成為屬性函數(shù),可以對(duì)屬性賦值時(shí)做必要的檢查,并保證代碼的清晰短小,主要有2個(gè)作用
將方法轉(zhuǎn)換為只讀
重新實(shí)現(xiàn)一個(gè)屬性的設(shè)置和讀取方法,可做邊界判定
class Money(object): def __init__(self): self.__money = 0 @property def money(self): return self.__money @money.setter def money(self, value): if isinstance(value, int): self.__money = value else: print("error:不是整型數(shù)字")
運(yùn)行結(jié)果
In [3]: a = Money() In [4]: In [4]: In [4]: a.money Out[4]: 0 In [5]: a.money = 100 In [6]: a.money Out[6]: 100
內(nèi)建屬性
"teachclass.py" class Person(object): pass
python3.5中類(lèi)的內(nèi)建屬性和方法
經(jīng)典類(lèi)(舊式類(lèi)),早期如果沒(méi)有要繼承的父類(lèi),繼承里空著不寫(xiě)的類(lèi)
#py2中無(wú)繼承父類(lèi),稱之經(jīng)典類(lèi),py3中已默認(rèn)繼承object class Person: pass
子類(lèi)沒(méi)有實(shí)現(xiàn)__init__方法時(shí),默認(rèn)自動(dòng)調(diào)用父類(lèi)的。 如定義__init__方法時(shí),需自己手動(dòng)調(diào)用父類(lèi)的__init__方法
__getattribute__例子:
class Itcast(object): def __init__(self,subject1): self.subject1 = subject1 self.subject2 = 'cpp' #屬性訪問(wèn)時(shí)攔截器,打log def __getattribute__(self,obj): if obj == 'subject1': print('log subject1') return 'redirect python' else: #測(cè)試時(shí)注釋掉這2行,將找不到subject2 return object.__getattribute__(self,obj) def show(self): print('this is Itcast') s = Itcast("python") print(s.subject1) print(s.subject2)
運(yùn)行結(jié)果:
log subject1 redirect python cpp
**__getattribute__的坑**
class Person(object): def __getattribute__(self,obj): print("---test---") if obj.startswith("a"): return "hahha" else: return self.test def test(self): print("heihei") t.Person() t.a #返回hahha t.b #會(huì)讓程序死掉 #原因是:當(dāng)t.b執(zhí)行時(shí),會(huì)調(diào)用Person類(lèi)中定義的__getattribute__方法,但是在這個(gè)方法的執(zhí)行過(guò)程中 #if條件不滿足,所以 程序執(zhí)行else里面的代碼,即return self.test 問(wèn)題就在這,因?yàn)閞eturn 需要把 #self.test的值返回,那么首先要獲取self.test的值,因?yàn)閟elf此時(shí)就是t這個(gè)對(duì)象,所以self.test就是 #t.test 此時(shí)要獲取t這個(gè)對(duì)象的test屬性,那么就會(huì)跳轉(zhuǎn)到__getattribute__方法去執(zhí)行,即此時(shí)產(chǎn) #生了遞歸調(diào)用,由于這個(gè)遞歸過(guò)程中 沒(méi)有判斷什么時(shí)候推出,所以這個(gè)程序會(huì)永無(wú)休止的運(yùn)行下去,又因?yàn)? #每次調(diào)用函數(shù),就需要保存一些數(shù)據(jù),那么隨著調(diào)用的次數(shù)越來(lái)越多,最終內(nèi)存吃光,所以程序 崩潰 # # 注意:以后不要在__getattribute__方法中調(diào)用self.xxxx
以上是“Python中屬性和內(nèi)建屬性的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁(yè)標(biāo)題:Python中屬性和內(nèi)建屬性的示例分析-創(chuàng)新互聯(lián)
文章源于:http://muchs.cn/article48/cdgchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、定制網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容