Python裝飾器中@property使用詳解
最初的聲明方式
在沒有@property修飾的情況下,需要分別聲明get、set、delete函數(shù),然后初始化property類,將這些方法加載進property中
class C持有property的實例化對象x
對外表現(xiàn)出來C().x時,實際上是調(diào)用C()中的x(property類)中設(shè)置的fset,fget,fdel,分別對應(yīng)getx,setx,delx
C真正持有的x,是self._x被隱藏起來了
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類 結(jié)合x = property(getx, setx, delx, "I'm the 'x' property.")
與property的__init__()
可以發(fā)現(xiàn)property接受四個參數(shù)
fget,用于獲取屬性值,
fset,用于設(shè)置屬性值
fdel,用于刪除屬性
doc,屬性的介紹
可以單獨設(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
使用裝飾器的聲明方式
需要注意,裝飾器只是一個python的語法糖,可以拆解成普通使用方法,如property(getx)
@property
創(chuàng)建了一個實例x,對于def x(self)
實際上是C類持有x = property(fget=x)
因此,x.setter
方法指向的是property.setter
,也是起到裝飾器效果x.setter(x)
(注意,前者x是property實例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實例化后的名字與屬性名一致?
換種問法就是為什么x = property(...)
可以認為是
attributes_and_methods = { x.__name__: property(x), //聲明C類持有property實例 #... } C = type('C', (object,), attributes_and_methods)
使用裝飾器的調(diào)用過程
執(zhí)行C().x時,調(diào)用的是C().x(property)綁定的fget方法,用過__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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 掌握Python property裝飾器巧妙管理類的屬性
- python裝飾器中@property屬性的使用解析
- Python中通過property設(shè)置類屬性的訪問
- 關(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ò)攝像機解析mjpeg http流的示例
這篇文章主要介紹了python3從網(wǎng)絡(luò)攝像機解析mjpeg http流的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11python?aeon庫進行時間序列算法預(yù)測分類實例探索
這篇文章主要介紹了python?aeon庫進行時間序列算法預(yù)測分類實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-02-02pandas.DataFrame的pivot()和unstack()實現(xiàn)行轉(zhuǎn)列
這篇文章主要介紹了pandas.DataFrame的pivot()和unstack()實現(xiàn)行轉(zhuǎn)列,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-07-07python 對txt中每行內(nèi)容進行批量替換的方法
今天小編就為大家分享一篇python 對txt中每行內(nèi)容進行批量替換的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07在Python中測試訪問同一數(shù)據(jù)的競爭條件的方法
這篇文章主要介紹了在Python中測試訪問同一數(shù)據(jù)的競爭條件的方法,探究多線程或多進程情況下優(yōu)先訪問權(quán)的問題,需要的朋友可以參考下2015-04-04