Python裝飾器中@property使用詳解
最初的聲明方式
在沒有@property修飾的情況下,需要分別聲明get、set、delete函數,然后初始化property類,將這些方法加載進property中
class C持有property的實例化對象x
對外表現出來C().x時,實際上是調用C()中的x(property類)中設置的fset,fget,fdel,分別對應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類 結合x = property(getx, setx, delx, "I'm the 'x' property.")與property的__init__()可以發(fā)現property接受四個參數
fget,用于獲取屬性值,
fset,用于設置屬性值
fdel,用于刪除屬性
doc,屬性的介紹
可以單獨設置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)函數),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)
使用裝飾器的調用過程
執(zhí)行C().x時,調用的是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)
總結
到此這篇關于Python裝飾器中@property使用詳解的文章就介紹到這了,更多相關Python飾器@property內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 掌握Python property裝飾器巧妙管理類的屬性
- python裝飾器中@property屬性的使用解析
- Python中通過property設置類屬性的訪問
- 關于python中@property的使用方法
- Python?property裝飾器使用案例介紹
- Python深入分析@property裝飾器的應用
- python 中的@property的用法詳解
- python中@Property屬性使用方法
- Python中property屬性的用處詳解
- Python中關于property使用的小技巧
- Python的@property的使用
- 詳解Python裝飾器之@property
- Python property函數的具體使用
相關文章
python numpy實現多次循環(huán)讀取文件 等間隔過濾數據示例
這篇文章主要介紹了python numpy實現多次循環(huán)讀取文件 等間隔過濾數據示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

