简介:

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:

python Descriptor (描述符)的更多相关文章

  1. python数据描述符

    Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题苦恼的朋友提 ...

  2. 【python】描述符descriptor

    开始看官方文档,各种看不懂,只看到一句Properties, bound and unbound methods, static methods, and class methods are all ...

  3. python理解描述符(descriptor)

    Descriptor基础 python中的描述符可以用来定义触发自动执行的代码,它像是一个对象属性操作(访问.赋值.删除)的代理类一样.前面介绍过的property是描述符的一种. 大致流程是这样的: ...

  4. python tips:描述符descriptor

    描述符(descriptor)是实现了__get__.__set__.__del__方法的类,进一步可以细分为两类: 数据描述符:实现了__get__和__set__ 非数据描述符:没有实现__set ...

  5. python - 数据描述符(class 内置 get/set/delete方法 )

    数据描述符(class 内置 get/set/del方法 ): # 什么是描述符 # 官方的定义:描述符是一种具有“捆绑行为”的对象属性.访问(获取.设置和删除)它的属性时,实际是调用特殊的方法(_g ...

  6. Python属性描述符(二)

    Python存取属性的方式特别不对等,通过实例读取属性时,通常返回的是实例中定义的属性,但如果实例未曾定义过该属性,就会获取类属性,而为实例的属性赋值时,通常会在实例中创建属性,而不会影响到类本身.这 ...

  7. python之描述符

    描述符是将某种特殊类型的类实例指派给另一个类的属性,某种特殊类型的类就是这个类里面封装了get,set,delete这三个方法,可以将这个类指派给另一个类的某一个属性,这样就可以通过这三个方法对该属性 ...

  8. Python属性描述符(一)

    描述符是对多个属性运用相同存取逻辑的一种方式,,是实现了特性协议的类,这个协议包括了__get__.__set__和__delete__方法.property类实现了完整的描述符协议.通常,可以只实现 ...

  9. Python的描述符

    1.描述符的定义 描述符是与特定属性互相绑定的一种协议,通过方法被触发修改属性,这些方法包括__get__(),__set__(),__delete__().将这些方法定义在类中,即可实现描述符 2. ...

随机推荐

  1. JMeter之自动重定向和跟随重定向的区别

    自动重定向:只针对Get和Head请求,自动重定向转向到最终目标页面,但是Jmeter不记录重定向的中间页面过程,只记录最终页面返回结果.在结果树中,只能看到最终页面的服务器返回. 跟随重定向:是ht ...

  2. vue小结

    一:MVVM模型的理解 Model:数据模型,数据和业务逻辑都在这里定义:View代表视图,负责数据的展示:ViewModel:负责监听model中数据的改变并且控制视图的更新,处理用户交互操作:Mo ...

  3. 【python路飞】编码 ascii码(256位 =1个字节)美国;unicode(万国码)中文 一共9万个 用4个字节表示这9万个子 17位就能表示

    8位一个字节  1024字节 1KB   1024KB 1MB ASCII码不能包含中文.创建了unicode,一个中文4个字节.UTF-8一个中文3个.GBK中国人用的只包含中文2个字节 升级 Un ...

  4. mysql中文查询问题

    alter table t_foo change `str` `str` varchar(100) character set utf8 not null ;

  5. windows 下安装redis

    https://github.com/MicrosoftArchive/redis/releases redis 服务安装到系统 redis-server.exe --service-install ...

  6. SELinux入门简介

    操作系统有两类访问控制:自主访问控制(DAC)和强制访问控制(MAC).标准Linux安全是一种DAC,SELinux为Linux增加了一个灵活的和可配置的的MAC. 进程启动时所拥有的权限就是运行此 ...

  7. java多线程系列 目录

    Java多线程系列1 线程创建以及状态切换    Java多线程系列2 线程常见方法介绍    Java多线程系列3 synchronized 关键词    Java多线程系列4 线程交互(wait和 ...

  8. linux简单安装方法

    一.配置静态IP NAT:模式: 修改网卡eth0 vim /etc/sysconfig/network-scripts/ifcfg-eth0 内容如下: DEVICE=eth0 HWADDR=:0C ...

  9. xml代码修改图片颜色

    <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http ...

  10. 检测2个url的不同之处(爬虫分析接口)

    就是简单的检测2个url的不同之处,在做爬虫时,要分析接口地址的不同之处,靠自己的眼睛有点累,所以写了一个小程序,不喜勿喷 #测试数据 a = "https://list.tmall.com ...