zoukankan      html  css  js  c++  java
  • python's descriptor II

    python's descriptor II

      For instance, a.x has a lookup chain starting with a.__dict__['x'], then type(a).__dict__['x'], and continuing through the base classes oftype(a) excluding metaclasses. If the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. Where this occurs in the precedence chain depends on which descriptor methods were defined. Note that descriptors are only invoked for new style objects or classes (a class is new style if it inherits from object or type).

      Descriptor are the mechanism behind properties, methods, static methods, class methods, and super().

      descr.__get__(self, obj, type=None) --> value

      descr.__set__(self, obj, value) --> None

      descr.__delete__(self, obj) --> None

      Define any of these methods and an object is considered a descriptor and can override default behavior upon being looked up as an attribute.

      If an object defines both __get__() and __set__(), it is considered a data descriptor. Descriptors that only define __get__()are called non-data descriptors (they are typically used for methods but other uses are possible).

      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.

      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 full C implementation can be found in PyObject_GenericGetAttr() in Objects/object.c.

      For classes, the machinery is in type.__getattribute__() which transforms B.x into B.__dict__['x'].__get__(None, B). In pure Python, it looks like:

      

    Property

      Calling property() is a succinct way of building a data descriptor that triggers function calls upon access to an attribute. Its signature is:

      

       更多请看参考。

    Functions & Methods

      method的__get__方法要的是否有obj、objtype来返回不同的值。

      

     参考:http://docs.python.org/2.7/howto/descriptor.html

  • 相关阅读:
    IBinder在进程之间传递一个对象的形式(一)
    Xaml在string(串)定义常量和处理空间
    c 有意思的数组初始化
    C 文件直接包含
    [面试技巧]16个经典面试问题回答思路
    centos6安装bt工具transmission
    clearcase 中一些概念和操作
    C/C++ Resources
    Linux I/O 重定向详解及应用实例
    c/c++ 直接使用动态库 dlopen
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3505227.html
Copyright © 2011-2022 走看看