網(wǎng)站首頁 編程語言 正文
最初的聲明方式
在沒有@property修飾的情況下,需要分別聲明get、set、delete函數(shù),然后初始化property類,將這些方法加載進(jìn)property中
class C持有property的實(shí)例化對(duì)象x
對(duì)外表現(xiàn)出來C().x時(shí),實(shí)際上是調(diào)用C()中的x(property類)中設(shè)置的fset,fget,fdel,分別對(duì)應(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接受四個(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的語法糖,可以拆解成普通使用方法,如property(getx)
@property
創(chuàng)建了一個(gè)實(shí)例x,對(duì)于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時(shí),調(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é)
原文鏈接:https://blog.csdn.net/ct2020129/article/details/122681130
相關(guān)推薦
- 2022-10-01 C語言五子棋小游戲?qū)崿F(xiàn)代碼_C 語言
- 2022-01-16 1.把字符串轉(zhuǎn)化為時(shí)間戳,再將時(shí)間戳轉(zhuǎn)化為Date對(duì)象 /** *@parame time = 2
- 2022-11-21 得物基于StarRocks的OLAP需求實(shí)踐詳解_數(shù)據(jù)庫其它
- 2022-11-06 react中braft-editor的基本使用方式_React
- 2022-05-12 Android Studio 崩潰一閃而過抓不到日志
- 2023-11-26 StringBuffer 和 StringBuilder
- 2022-11-19 Python變量和數(shù)據(jù)類型和數(shù)據(jù)類型的轉(zhuǎn)換_python
- 2022-10-27 Python中對(duì)字典的幾個(gè)處理方法分享_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支