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

  • 相关阅读:
    C++实现反射
    ubuntu下安装secureCRT(含破解方法)
    2018 年力扣高频算法面试题汇总-难题记录-鸡蛋掉落
    对于opencv全面貌的认识和理解
    关于c++类的一些知识的总结
    vs2017+opencv4.0.1安装配置详解(win10)
    leetcode-120-三角形最小路径和
    leetcode-64-最小路径和
    leetcode-917-仅仅反转字母
    leetcode-914-卡牌分组
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3505227.html
Copyright © 2011-2022 走看看