python Descriptor (描述符)
简介:
python 描述符是新式类(继承自object)中的语言协议,基于描述符可以提供更佳优雅的解决方案。
python的classmethod, staticmethod, property都是基于描述符建立的。
描述符的协议:
定义了__set__, __get__, __delete__3个方法中任何一个方法的object可以作为描述符.
描述符分类:
同时定义了__set__,__get__被叫做data descriptor.
只定义了__get__被叫做no-data descriptor.
2种描述符的区别:
Data and non-data descriptors differ in how overrides are calculated with respect to entries in an instance’s dictionary. If an instance’s dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance’s dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence.
在 attrubuite lookup过程中 :
如果对象属性有与data-descriptor同名的属性,data-descriptor优先于对象属性.
如果对象属性有与no-data descriptor同名的属性,对象属性优先。
触发描述符的调用:
A descriptor can be called directly by its method name. For example, d.__get__(obj).
Alternatively, it is more common for a descriptor to be invoked automatically upon attribute access. For example, obj.d looks up d in the dictionary of obj. If d defines the method __get__(), thend.__get__(obj) is invoked according to the precedence rules listed below.
The details of invocation depend on whether obj is an object or a class. Either way, descriptors only work for new style objects and classes. A class is new style if it is a subclass of object.
For objects, the machinery is in object.__getattribute__() which transforms b.x into type(b).__dict__['x'].__get__(b, type(b)). The implementation works through a precedence chain that gives data descriptors priority over instance variables, instance variables priority over non-data descriptors, and assigns lowest priority to __getattr__() if provided
The important points to remember are:
- descriptors are invoked by the
__getattribute__()method - overriding
__getattribute__()prevents automatic descriptor calls __getattribute__()is only available with new style classes and objectsobject.__getattribute__()andtype.__getattribute__()make different calls to__get__().- data descriptors always override instance dictionaries.
- non-data descriptors may be overridden by instance dictionaries.
python Descriptor (描述符)的更多相关文章
- python数据描述符
Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题苦恼的朋友提 ...
- 【python】描述符descriptor
开始看官方文档,各种看不懂,只看到一句Properties, bound and unbound methods, static methods, and class methods are all ...
- python理解描述符(descriptor)
Descriptor基础 python中的描述符可以用来定义触发自动执行的代码,它像是一个对象属性操作(访问.赋值.删除)的代理类一样.前面介绍过的property是描述符的一种. 大致流程是这样的: ...
- python tips:描述符descriptor
描述符(descriptor)是实现了__get__.__set__.__del__方法的类,进一步可以细分为两类: 数据描述符:实现了__get__和__set__ 非数据描述符:没有实现__set ...
- python - 数据描述符(class 内置 get/set/delete方法 )
数据描述符(class 内置 get/set/del方法 ): # 什么是描述符 # 官方的定义:描述符是一种具有“捆绑行为”的对象属性.访问(获取.设置和删除)它的属性时,实际是调用特殊的方法(_g ...
- Python属性描述符(二)
Python存取属性的方式特别不对等,通过实例读取属性时,通常返回的是实例中定义的属性,但如果实例未曾定义过该属性,就会获取类属性,而为实例的属性赋值时,通常会在实例中创建属性,而不会影响到类本身.这 ...
- python之描述符
描述符是将某种特殊类型的类实例指派给另一个类的属性,某种特殊类型的类就是这个类里面封装了get,set,delete这三个方法,可以将这个类指派给另一个类的某一个属性,这样就可以通过这三个方法对该属性 ...
- Python属性描述符(一)
描述符是对多个属性运用相同存取逻辑的一种方式,,是实现了特性协议的类,这个协议包括了__get__.__set__和__delete__方法.property类实现了完整的描述符协议.通常,可以只实现 ...
- Python的描述符
1.描述符的定义 描述符是与特定属性互相绑定的一种协议,通过方法被触发修改属性,这些方法包括__get__(),__set__(),__delete__().将这些方法定义在类中,即可实现描述符 2. ...
随机推荐
- JS-正则表达式 限制输入整数、小数
//只可以输入整数 onkeyup="value=value.replace(/[^\d]/g,'')" //可以输入数字 包括小数 onkeyup="value=val ...
- c#: WebBrowser 禁止在新窗口打开链接
项目中碰到此需求.几番比对,此为最好的解决方案,聊做备忘. 1.加入Microsoft Internet Controls引用: 项目右键->添加引用->COM->Microsoft ...
- jdk和tomcat的安装
https://blog.csdn.net/angel_w/article/details/78580528
- iOS相关的ARM汇编
一.iOS汇编1.真机:arm64汇编寄存器指令 堆栈2.模拟器:x86汇编 二.lldb (lldb)register read x0 (lldb)register read w0 (lldb)re ...
- js千分位加逗号
code function toThousands(num) { var numArr = num.split('.'); num = numArr[0]; var result = ''; whil ...
- unity导入TexturePacker处理
1.从Asset Store里下载TexturePackerImporter ,然后导入到项目中. 2.导入unity的一张大图和一个.tpsheet文件(注意原始图片也要在相同目录) 3.代码导入 ...
- mysql学习--1.事务
转载自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/3984001.html 四.事务的四大特性(ACID) 4.1.原子性(Atomicity) 原子性是指事务 ...
- docker镜像运行错误排查
docker做服务时,如果客户端无法连接,错误排查: 1.先使用 docker ps 查看镜像是否都在运行中,如果没有就进入镜像查看日志 2.如果确定代码及配置文件没有问题,就需要检查镜像的替换是否正 ...
- python基础之Day22
1.组合 什么是? 一个类的对象具备某一个属性,该属性值属于另一个类的对象,这样就可以引用 为何用: 解决类与类之间代码冗余问题 如何用? 2.菱形继承 单继承:一个个往父类上查找 菱形:一个子类继承 ...
- NC 创建表空间数据库
1.首先创建表空间 CREATE SMALLFILE TABLESPACE "NNC_DATA01" LOGGING DATAFILE 'D:\NCOracle\nc633sd\N ...