Python描述符descriptor使用原理解析
作者:luoheng 时间:2022-03-24 20:12:48
描述符(descriptor)是实现了__get__、__set__、__del__方法的类,进一步可以细分为两类:
数据描述符:实现了__get__和__set__
非数据描述符:没有实现__set__
描述符在类的属性调用中起着很重要的作用,类在调用属性时,遵守两个规则:
按照实例属性、类属性的顺序选择属性,即实例属性优先于类属性
如果在类属性中发现同名的数据描述符,那么该描述符会优先于实例属性
非数据描述符会被实例属性覆盖
class A:
def __get__(self, obj, cls):
return f"{obj}: get"
class B:
value = A()
def __init__(self):
self.value = 4
def main():
g = B()
print(g.value)
print(g.__dict__)
if __name__ == "__main__":
main()
输出结果
4
{'value': 4}
数据描述符优于实例属性
class A:
def __get__(self, obj, cls):
return f"{obj}: get"
def __set__(self, obj, value):
print(f"{obj}: set, {value}")
class B:
value = A()
def __init__(self):
self.value = 4
def main():
g = B()
print(g.value)
print(g.__dict__)
if __name__ == "__main__":
main()
输出结果
<__main__.B object at 0x000001165EB85898>: set, 4
<__main__.B object at 0x000001165EB85898>: get
{}
从上述两个例子中可以看到,类B的value属性是一个描述符,当value属性是一个数据描述符时,它屏蔽了实例的同名属性value,实例对value属性的读取与赋值都会直接被转移到类属性value上。
使用描述符实现类的静态方法与类方法
from functools import partial
class Staticmethod:
def __init__(self, method):
self.method = method
def __get__(self, obj, cls):
return self.method
class Classmethod:
def __init__(self, method):
self.method = method
def __get__(self, obj, cls):
return partial(self.method, cls)
class A:
@Staticmethod
def f(self):
print(f"I'm method f, the value is {self}")
@Classmethod
def c(self):
print(f"my class is {self}")
a = A()
a.f(23)
A.f(23)
a.c()
A.c()
输出结果
I'm method f, the value is 23
I'm method f, the value is 23
my class is <class '__main__.A'>
my class is <class '__main__.A'>
静态方法与类方法统一了类属性的两种引用方式。这种统一的过程可以使用描述符修改属性访问的默认方式实现。静态方法限制实例的默认绑定,将方法当做普通函数使用;类方法始终将类作为第一个参数传入,上述的partial将类固定为方法的第一个参数。
总结
描述符是实现了__get__、__set__、__del__等特殊方法的类,在属性访问时起着很大的作用。
数据描述符会覆盖同名的实例属性,通过使用数据描述符,达到通过实例修改类变量的目的。
描述符用于修改属性的默认访问方式,借此可以实现类方法与静态方法。
来源:https://www.cnblogs.com/luoheng23/p/11083398.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中的集合介绍
![](https://img.aspxhome.com/file/2023/1/101111_0s.png)
python中py文件与pyc文件相互转换的方法实例
![](https://img.aspxhome.com/file/2023/5/72165_0s.png)
Python绘制K线图之可视化神器pyecharts的使用
![](https://img.aspxhome.com/file/2023/5/95415_0s.png)
聊聊python中令人迷惑的duplicated和drop_duplicates()用法
![](https://img.aspxhome.com/file/2023/4/87584_0s.png)
Windows 8.1 64bit下搭建 Scrapy 0.22 环境
![](https://img.aspxhome.com/file/2023/7/64327_0s.png)
python opencv3实现人脸识别(windows)
PHP中soap的用法实例
如何解决客户机页面刷新时连接不上数据库问题?
python numpy 中linspace函数示例详解
![](https://img.aspxhome.com/file/2023/7/86177_0s.png)
pytest实现多进程与多线程运行超好用的插件
![](https://img.aspxhome.com/file/2023/0/99510_0s.jpg)
深入浅析python3 依赖倒置原则(示例代码)
Python中使用item()方法遍历字典的例子
![](https://img.aspxhome.com/file/2023/5/85325_0s.jpg)
浅谈Python脚本开头及导包注释自动添加方法
![](https://img.aspxhome.com/file/2023/5/115665_0s.jpg)
Python中字典和集合学习小结
使用Python创建简单的HTTP服务器的方法步骤
python base64库给用户名或密码加密的流程
如何检测用户第一次访问我的网站并显示友好信息?
SQLServer中数据库文件的存放方式,文件和文件组
Python Pandas中DataFrame.drop_duplicates()删除重复值详解
![](https://img.aspxhome.com/file/2023/6/75906_0s.png)
python爬虫 基于requests模块发起ajax的get请求实现解析
![](https://img.aspxhome.com/file/2023/9/69029_0s.png)