Python裝飾器中@property使用詳解
最初的聲明方式
在沒(méi)有@property修飾的情況下,需要分別聲明get、set、delete函數(shù),然后初始化property類(lèi),將這些方法加載進(jìn)property中
class C持有property的實(shí)例化對(duì)象x
對(duì)外表現(xiàn)出來(lái)C().x時(shí),實(shí)際上是調(diào)用C()中的x(property類(lèi))中設(shè)置的fset,fget,fdel,分別對(duì)應(yīng)getx,setx,delx
C真正持有的x,是self._x被隱藏起來(lái)了
class C(object): def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, "I'm the 'x' property.")
property類(lèi) 結(jié)合x = property(getx, setx, delx, "I'm the 'x' property.")
與property的__init__()
可以發(fā)現(xiàn)property接受四個(gè)參數(shù)
fget,用于獲取屬性值,
fset,用于設(shè)置屬性值
fdel,用于刪除屬性
doc,屬性的介紹
可以單獨(dú)設(shè)置fget、fset、fdel…
x = property,x.getter(getx),x.setter(setx),x.deleter(delx)
class property(object): def deleter(self, *args, **kwargs): # real signature unknown """ Descriptor to change the deleter on a property. """ pass def getter(self, *args, **kwargs): # real signature unknown """ Descriptor to change the getter on a property. """ pass def setter(self, *args, **kwargs): # real signature unknown """ Descriptor to change the setter on a property. """ pass def __delete__(self, *args, **kwargs): # real signature unknown """ Delete an attribute of instance. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __get__(self, *args, **kwargs): # real signature unknown """ Return an attribute of instance, which is of type owner. """ pass def __init__(self, fget=None, fset=None, fdel=None, doc=None): # known special case of pass
使用裝飾器的聲明方式
需要注意,裝飾器只是一個(gè)python的語(yǔ)法糖,可以拆解成普通使用方法,如property(getx)
@property
創(chuàng)建了一個(gè)實(shí)例x,對(duì)于def x(self)
實(shí)際上是C類(lèi)持有x = property(fget=x)
因此,x.setter
方法指向的是property.setter
,也是起到裝飾器效果x.setter(x)
(注意,前者x是property實(shí)例x,后者x是def x(self, value)
函數(shù)),x.deleter
同理
class C(object): @property def x(self): "I am the 'x' property." return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x
為什么property實(shí)例化后的名字與屬性名一致?
換種問(wèn)法就是為什么x = property(...)
可以認(rèn)為是
attributes_and_methods = { x.__name__: property(x), //聲明C類(lèi)持有property實(shí)例 #... } C = type('C', (object,), attributes_and_methods)
使用裝飾器的調(diào)用過(guò)程
執(zhí)行C().x時(shí),調(diào)用的是C().x(property)綁定的fget方法,用過(guò)__get__
喚醒,setter、deleter同理
class property(object): #... def __init__(self, fget=None, fset=None, fdel=None, doc=None): self.fget = fget self.fset = fset self.fdel = fdel ... def __get__(self, obj, objtype=None): # real signature unknown if obj is None: return self if self.fget is None: raise AttributeError("unreadable attribute") return self.fget(obj)
總結(jié)
到此這篇關(guān)于Python裝飾器中@property使用詳解的文章就介紹到這了,更多相關(guān)Python飾器@property內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 掌握Python property裝飾器巧妙管理類(lèi)的屬性
- python裝飾器中@property屬性的使用解析
- Python中通過(guò)property設(shè)置類(lèi)屬性的訪(fǎng)問(wèn)
- 關(guān)于python中@property的使用方法
- Python?property裝飾器使用案例介紹
- Python深入分析@property裝飾器的應(yīng)用
- python 中的@property的用法詳解
- python中@Property屬性使用方法
- Python中property屬性的用處詳解
- Python中關(guān)于property使用的小技巧
- Python的@property的使用
- 詳解Python裝飾器之@property
- Python property函數(shù)的具體使用
相關(guān)文章
python3從網(wǎng)絡(luò)攝像機(jī)解析mjpeg http流的示例
這篇文章主要介紹了python3從網(wǎng)絡(luò)攝像機(jī)解析mjpeg http流的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11python?aeon庫(kù)進(jìn)行時(shí)間序列算法預(yù)測(cè)分類(lèi)實(shí)例探索
這篇文章主要介紹了python?aeon庫(kù)進(jìn)行時(shí)間序列算法預(yù)測(cè)分類(lèi)實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02python 實(shí)現(xiàn)ping測(cè)試延遲的兩種方法
這篇文章主要介紹了python 實(shí)現(xiàn)ping測(cè)試延遲的兩種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12pandas.DataFrame的pivot()和unstack()實(shí)現(xiàn)行轉(zhuǎn)列
這篇文章主要介紹了pandas.DataFrame的pivot()和unstack()實(shí)現(xiàn)行轉(zhuǎn)列,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-07-07python 對(duì)txt中每行內(nèi)容進(jìn)行批量替換的方法
今天小編就為大家分享一篇python 對(duì)txt中每行內(nèi)容進(jìn)行批量替換的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07在Python中測(cè)試訪(fǎng)問(wèn)同一數(shù)據(jù)的競(jìng)爭(zhēng)條件的方法
這篇文章主要介紹了在Python中測(cè)試訪(fǎng)問(wèn)同一數(shù)據(jù)的競(jìng)爭(zhēng)條件的方法,探究多線(xiàn)程或多進(jìn)程情況下優(yōu)先訪(fǎng)問(wèn)權(quán)的問(wèn)題,需要的朋友可以參考下2015-04-04python隨機(jī)生成庫(kù)faker庫(kù)api實(shí)例詳解
今天小編就為大家分享一篇python隨機(jī)生成庫(kù)faker庫(kù)api實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11