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

  • 相关阅读:
    搭建React+TypeScript项目
    vue 基础常用部分总结
    nodejs基于nodemailer插件实现发送邮箱
    httpserver的使用
    android4.0.4 系统默认值的修改
    使用TransferLearning实现环视图像的角点检测——Tensorflow+MobileNetv2_SSD
    Idea 2020.3 springboot 热更新
    Prism学习之SilverlightWindowRegionAdapter
    Silverlight异步Socket通信
    Lync 2010 标准版安装注意事项
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3505227.html
Copyright © 2011-2022 走看看