python描述符的兩種類型
python描述符的兩種類型
1、分類
數(shù)據(jù)描述符:實(shí)現(xiàn)了__get__和__set__兩種方法的描述符
非數(shù)據(jù)描述符:只實(shí)現(xiàn)了__get__一種方法的描述符
2、實(shí)例
#數(shù)據(jù)描述符
classDataDes:
def__init__(self,default=0):
self._score=default
def__set__(self,instance,value):
self._score=value
def__get__(self,instance,owner):
print("訪問數(shù)據(jù)描述符里的__get__")
returnself._score
#非數(shù)據(jù)描述符
classNoDataDes:
def__init__(self,default=0):
self._score=default
def__get__(self,instance,owner):
print("訪問非數(shù)據(jù)描述符里的__get__")
returnself._score
classStudent:
math=DataDes(0)
chinese=NoDataDes(0)
def__init__(self,name,math,chinese):
self.name=name
self.math=math
self.chinese=chinese
def__getattribute__(self,item):
print("調(diào)用__getattribute__")
returnsuper(Student,self).__getattribute__(item)
def__repr__(self):
return"".format(
self.name,self.math,self.chinese)
以上就是Python描述符的兩種類型,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)推薦:請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。

相關(guān)推薦HOT
更多>>
python函數(shù)裝飾器的應(yīng)用過程
python函數(shù)裝飾器的應(yīng)用過程1、應(yīng)用過程一般是定義在一個(gè)獨(dú)立模塊中,使用時(shí)會(huì)被導(dǎo)入到其它模塊;函數(shù)裝飾器會(huì)在內(nèi)部定義一個(gè)新函數(shù),然后返回。...詳情>>
2023-11-10 22:33:56
python實(shí)現(xiàn)異步的兩種框架
python實(shí)現(xiàn)異步的兩種框架Python實(shí)現(xiàn)異步的框架有很多,但是核心思想大概是基于下面兩種方式twistergeventtwister1、twistertwister思想是將異...詳情>>
2023-11-10 22:11:26
python函數(shù)形式如何實(shí)現(xiàn)生成器
python函數(shù)形式如何實(shí)現(xiàn)生成器1、說明生成器也是迭代器,但只能迭代一次。這是因?yàn)樗鼈儧]有存儲(chǔ)所有的值,而是在運(yùn)行過程中產(chǎn)生值。你一直在使...詳情>>
2023-11-10 21:50:58
python描述符的兩種類型
python描述符的兩種類型1、分類數(shù)據(jù)描述符:實(shí)現(xiàn)了__get__和__set__兩種方法的描述符非數(shù)據(jù)描述符:只實(shí)現(xiàn)了__get__一種方法的描述符2、實(shí)例#數(shù)...詳情>>
2023-11-10 21:30:01熱門推薦
pythonLogging是什么?
沸python如何應(yīng)用于數(shù)據(jù)的基礎(chǔ)統(tǒng)計(jì)分析
熱python模塊的name屬性
熱pythongRPC是什么?
新python函數(shù)裝飾器的應(yīng)用過程
python實(shí)現(xiàn)異步的兩種框架
python函數(shù)形式如何實(shí)現(xiàn)生成器
python描述符的兩種類型
python__setattr__的屬性設(shè)置
pythonidle 是什么
pythonheapq是什么
python調(diào)試器是什么
pythonpsutil是什么
python創(chuàng)建新線程有哪些方法
技術(shù)干貨







快速通道 更多>>
-
課程介紹
點(diǎn)擊獲取大綱 -
就業(yè)前景
查看就業(yè)薪資 -
學(xué)習(xí)費(fèi)用
了解課程價(jià)格 -
優(yōu)惠活動(dòng)
領(lǐng)取優(yōu)惠券 -
學(xué)習(xí)資源
領(lǐng)3000G教程 -
師資團(tuán)隊(duì)
了解師資團(tuán)隊(duì) -
實(shí)戰(zhàn)項(xiàng)目
獲取項(xiàng)目源碼 -
開班地區(qū)
查看來校路線