python3 property裝飾器實(shí)現(xiàn)原理與用法示例
本文實(shí)例講述了python3 property裝飾器實(shí)現(xiàn)原理與用法。分享給大家供大家參考,具體如下:
學(xué)習(xí)python的同學(xué),慢慢的都會(huì)接觸到裝飾器,裝飾器在python里是功能強(qiáng)大的語(yǔ)法。裝飾器配合python的魔法方法,能實(shí)現(xiàn)很多意想不到的功能。廢話不多說(shuō),如果你已經(jīng)掌握了閉包的原理,代碼的邏輯還是可以看明白的,咱們直接進(jìn)入正題。
property的意義
@property把一個(gè)類的getter方法變成屬性,如果還有setter方法,就在setter方法前面加上@method.setter。使用類屬性=property(getx,setx,delx,desc)也是可以的。
實(shí)現(xiàn)很簡(jiǎn)單,那么它背后的原理是什么呢?
Property類的偽代碼如下,里面涉及了__get__、__set__、__delete__魔法方法。Decorator類是裝飾器類,Target是目標(biāo)類。當(dāng)你設(shè)置裝飾器類的實(shí)例對(duì)象為目標(biāo)類的x屬性后,當(dāng)試圖訪問(wèn)目標(biāo)類的x屬性會(huì)觸發(fā)裝飾器類的__get__方法;當(dāng)為目標(biāo)類的x屬性賦值時(shí),會(huì)觸發(fā)裝飾器類的__setter__方法;嘗試刪除目標(biāo)類的x屬性時(shí),會(huì)觸發(fā)裝飾器類的__delete__方法。當(dāng)訪問(wèn)Target.x.__doc__,可以打印出裝飾器類的描述文檔。事實(shí)上這種裝飾器類也被稱為描述符類。描述符類就是將一個(gè)特殊類的實(shí)例指派給一個(gè)類的屬性。
類屬性實(shí)現(xiàn)方式:
class Decorator(object):
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc
def __get__(self, instance, owner):
if instance is None:
return self
return self.fget(instance)
def __set__(self, instance, value):
self.fset(instance, value)
def __delete__(self, instance):
self.fdel(instance)
def getter(self, fget):
return Decorator(fget, self.fset, self.fdel, self.__doc__)
def setter(self, fset):
return Decorator(self.fget, fset, self.fdel, self.__doc__)
def deleter(self, fdel):
return Decorator(self.fget, self.fset, fdel, self.__doc__)
class Target(object):
desc = "Amazing pyhton"
def __init__(self, attr=5):
self._x = attr
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = Decorator(getx,setx,delx,desc)
裝飾器實(shí)現(xiàn)方式:
class Decorator(object):
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc
def __get__(self, instance, owner):
if instance is None:
return self
return self.fget(instance)
def __set__(self, instance, value):
self.fset(instance, value)
def __delete__(self, instance):
self.fdel(instance)
def getter(self, fget):
return Decorator(fget, self.fset, self.fdel, self.__doc__)
def setter(self, fset):
return Decorator(self.fget, fset, self.fdel, self.__doc__)
def deleter(self, fdel):
return Decorator(self.fget, self.fset, fdel, self.__doc__)
class Target(object):
desc = "Amazing pyhton"
def __init__(self, attr=5):
self._x = attr
@Decorator
def show(self):
return self._x
@show.setter
def show(self, value):
self._x = value
@show.deleter
def show(self):
del self._x
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python特殊字符作為字符串不轉(zhuǎn)義的問(wèn)題
這篇文章主要介紹了python特殊字符作為字符串不轉(zhuǎn)義的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
python實(shí)現(xiàn)簡(jiǎn)單點(diǎn)對(duì)點(diǎn)(p2p)聊天
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單點(diǎn)對(duì)點(diǎn)p2p聊天,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
python學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)實(shí)例代碼
數(shù)據(jù)結(jié)構(gòu)就是用來(lái)將數(shù)據(jù)組織在一起的結(jié)構(gòu)。換句話說(shuō),數(shù)據(jù)結(jié)構(gòu)是用來(lái)存儲(chǔ)一系列關(guān)聯(lián)數(shù)據(jù)的東西。在Python中有四種內(nèi)建的數(shù)據(jù)結(jié)構(gòu),分別是List、Tuple、Dictionary以及Set。本文將通過(guò)實(shí)例來(lái)介紹這些數(shù)據(jù)結(jié)構(gòu)的用法。2015-05-05
Python基類函數(shù)的重載與調(diào)用實(shí)例分析
這篇文章主要介紹了Python基類函數(shù)的重載與調(diào)用方法,實(shí)例分析了Python中基類函數(shù)的重載及調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
如何利用python?turtle繪圖自定義畫(huà)布背景顏色
Turtle庫(kù)是Python語(yǔ)言中一個(gè)很流行的繪制圖像的函數(shù)庫(kù),想象一個(gè)小烏龜,在一個(gè)橫軸為x、縱軸為y的坐標(biāo)系原點(diǎn),(0,0)位置開(kāi)始,這篇文章主要給大家介紹了關(guān)于如何利用python?turtle繪圖自定義畫(huà)布背景顏色的相關(guān)資料,需要的朋友可以參考下2021-12-12
import?paddle報(bào)錯(cuò)的成功解決方法
最近安裝paddle的時(shí)候遇到了些問(wèn)題,這里給大家總結(jié)下,下面這篇文章主要給大家介紹了關(guān)于import?paddle報(bào)錯(cuò)的成功解決方法,需要的朋友可以參考下2023-06-06
Seaborn數(shù)據(jù)分析NBA球員信息數(shù)據(jù)集
這篇文章主要為大家介紹了Seaborn數(shù)據(jù)分析處理NBA球員信息數(shù)據(jù)集案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Pygame實(shí)戰(zhàn)練習(xí)之飛機(jī)大戰(zhàn)游戲
飛機(jī)大戰(zhàn)想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂(lè)的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫(xiě)一個(gè)簡(jiǎn)單的飛機(jī)大戰(zhàn)小游戲的相關(guān)資料,需要的朋友可以參考下2021-09-09
python執(zhí)行js腳本報(bào)錯(cuò)CryptoJS is not defined問(wèn)題
這篇文章主要介紹了python執(zhí)行js腳本報(bào)錯(cuò)CryptoJS is not defined問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

