python's descriptor II

时间:2022-09-06 17:02:03

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:

  python's descriptor II

Property

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

  python's descriptor II

   更多请看参考。

Functions & Methods

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

  python's descriptor II

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

python's descriptor II的更多相关文章

  1. python初步要点II

    [python初步要点II] 1.is & is not 操作符用于测试2个对象是否指向同一个对象,即 id(a) == id(b). 2.整形和字符串对象是不可变对象,python会高效地缓 ...

  2. python's descriptor

    [python's descriptor] 1.实现了以下三个方法任意一个的,且作为成员变量存在的对象,就是descriptor. 1)object.__get__(self, instance, o ...

  3. python中descriptor的应用

    [python中descriptor的应用] 1.classmethod. 1)classmethod的应用. 2)classmethod原理. 2.staticmethod. 1)staticmet ...

  4. #3使用html+css+js制作网页 番外篇 使用python flask 框架 (II)

    #3使用html+css+js制作网页 番外篇 使用python flask 框架 II第二部 0. 本系列教程 1. 登录功能准备 a.python中操控mysql b. 安装数据库 c.安装mys ...

  5. Python的descriptor (2)

    前面说了descriptor,这个东西其实和Java的setter,getter有点像.但这个descriptor和上文中我们开始提到的函数方法这些东西有什么关系呢? 所有的函数都可以是descrip ...

  6. Python的Descriptor和Property混用

    一句话,把Property和Descriptor作用在同一个名字上,就只有Property好使.

  7. [Leetcode][Python]52: N-Queens II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...

  8. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  9. python advanced programming ( II )

    面向对象编程 简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数.数据封装.继承和多态是面向对象的三大特点. 在Python中,所有数据类型都可以视为对 ...

随机推荐

  1. 高通camera学习笔记

    http://www.2cto.com/kf/201609/548725.html http://www.android100.org/html/201508/24/176303.html

  2. 让编辑器支持word的复制黏贴,支持截屏的黏贴

    chrome有很多人性化的API,比如拖拽, 比如图片可以转化为base64等: 比如知乎上面的回复中可以直接黏贴图片,  就不需要手动点击图片上传按钮, 选择图片, 确认上传等等: 知乎参考地址:打 ...

  3. SQL server 表数据改变触发发送邮件

    今天遇到一个问题,原有生产系统正在健康运行,现需要监控一张数据表,当增加数据的时候,给管理员发送邮件. 领到这个需求后,有同事提供方案:写触发器触发外部应用程序.这是个大胆的想法啊,从来没写过这样的触 ...

  4. Android Animation 动画Demo(Frame帧动画)

    上一页介绍Animation动画第一:Tween吐温动画. 本文介绍了以下Animation也有动画的形式:Frame帧动画. Frame动画是一系列照片示出的顺序按照一定的处理,和机制,以放电影很阶 ...

  5. JavaScript递归原理

    JavaScript递归是除了闭包以外,函数的又一特色呢.很多开发新手都很难理解递归的原理,我在此总结出自己对递归的理解. 所谓递归,可以这样理解,就是一个函数在自身的局部环境里通过自身函数名又调用, ...

  6. Eclipse+Maven整合开发Java项目(二)➣webapp3.0以上的Maven项目

    概述 Eclipse集成Maven插件,新建maven-archetype-webapp项目的时候,采用的webapp的版本较低,默认是2.3,有些时候,我们希望升级Webapp的版本到3.0(Tom ...

  7. [Artoolkit] kpmMatching & Tracking of nftSimple

    1. kpmMatching thread main() --> loadNFTData() --> trackingInitInit() --> In static void *t ...

  8. 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context}Setting property 'source' to 'org.eclipse

    当你用Eclipse运行web项目的时候,你就会看到控制台出现:WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Set ...

  9. C# 用 * 输出两个等腰三角形组成的菱形

    int temp = 0; int n = 5; for(int i=0;i<n;i++){ for(int j=0;j<n-i;j++){ System.Console.Write(&q ...

  10. oracle系统包——dbms&lowbar;alert用法

    oracle内部提供的在数据库内部和应用程序间通信的方式有以下几种:1.警报,就是DBMS_ALERT包提供的功能:2.管道,由DBMS_PIPE提供:3.高级队列,这个就很复杂,当然提供的功能也是很 ...