Python裝飾器中@property使用詳解
最初的聲明方式
在沒有@property修飾的情況下,需要分別聲明get、set、delete函數(shù),然后初始化property類,將這些方法加載進(jìn)property中
class C持有property的實(shí)例化對象x
對外表現(xiàn)出來C().x時,實(shí)際上是調(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,屬性的介紹
可以單獨(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
使用裝飾器的聲明方式
需要注意,裝飾器只是一個python的語法糖,可以拆解成普通使用方法,如property(getx)
@property創(chuàng)建了一個實(shí)例x,對于def x(self)實(shí)際上是C類持有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í)例化后的名字與屬性名一致?
換種問法就是為什么x = property(...)
可以認(rèn)為是
attributes_and_methods = {
x.__name__: property(x), //聲明C類持有property實(shí)例
#...
}
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)文章
Python實(shí)現(xiàn)線性判別分析(LDA)的MATLAB方式
今天小編大家分享一篇Python實(shí)現(xiàn)線性判別分析(LDA)的MATLAB方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
python numpy實(shí)現(xiàn)多次循環(huán)讀取文件 等間隔過濾數(shù)據(jù)示例
這篇文章主要介紹了python numpy實(shí)現(xiàn)多次循環(huán)讀取文件 等間隔過濾數(shù)據(jù)示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python中的數(shù)據(jù)類dataclass解讀
這篇文章主要介紹了Python中的數(shù)據(jù)類dataclass使用,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

