zoukankan      html  css  js  c++  java
  • 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:

  • 相关阅读:
    学习、发现和创造一切皆有规律
    Ubuntu12.04下建立交叉编译环境、使用QEMU模拟CortexA9、QEMU运行uboot
    基于ARM的SoC设计入门[zz]
    ARM指令集详解[zz]
    电子工程自学步骤与书籍非电子专业
    IC设计的前端和后端[zz]
    [转]用C#获取IE临时文件
    二行代码解决全部网页木马(含iframe/script木马)(zt)
    winform 分页控件,源码下载
    在UpdatePanel中GridView导出EXECL问题
  • 原文地址:https://www.cnblogs.com/tulianghui/p/6692294.html
Copyright © 2011-2022 走看看