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

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. golang 常用网址收藏

    1:beego 模板语法指南:http://blog.go-china.org/03-beego-template 2:go 语言sublimetext2配置:http://www.kankanews ...

  2. Python3 学习第六弹: 迭代器与生成器

    1> 迭代器 迭代的意思类似递归一般,不断地对一个对象做重复的操作.来看个例子: class Fibs: def __init__(self): self.last = self.now = 1 ...

  3. laravel小抄

    原文地址:http://cheats.jesse-obrien.ca/ Artisan // Displays help for a given command php artisan --help ...

  4. java中进程与线程的三种实现方式

    一:进程与线程 概述:几乎任何的操作系统都支持运行多个任务,通常一个任务就是一个程序,而一个程序就是一个进程.当一个进程运行时,内部可能包括多个顺序执行流,每个顺序执行流就是一个线程. 进程:进程是指 ...

  5. MVC+Ef项目(4) 抽象业务逻辑层BLL层

    接下来,我们就要到业务逻辑层了,简单的说,业务逻辑层就是调用Repository(可以看做是DAL数据库访问层) 先来看看项目的架构 我们现在就开始来做BLL层.  同样,先编写  UserInfoS ...

  6. 多线程-NSOperation中使用ASIHttpRequest注意事项

    最近做的iPhone项目中有一如下功能: app在用户许可后将本地Photos的照片上传到服务器,期间用户可以做其他任何操作,等上传成功后弹出一个toast通知用户. 原先的代码结构是: 获取照片的操 ...

  7. iPad中控制器view初始的width和height

    1> 规律 * width 是宽高中最小的那个值 * height 是宽高中最大的那个值 2> 举例(比如窗口根控制器的view,有状态栏的情况下) * 横屏  width = 748,h ...

  8. Calculate drive total/free/available space

    using System; using System.Collections.Generic; using System.IO; using System.Text; namespace Consol ...

  9. 20160124.CCPP详解体系(0003天)

    程序片段(01):HelloCGI.c 内容概要:CGI_HelloWorld #include <stdio.h> //01.CGI程序的编写规范 // (1).HTML文本格式声明后面 ...

  10. 【解题报告】PKU 2826 An Easy Problem?!

    原题链接:http://poj.org/problem?id=2826 一题很蛋疼的一题.目前为止还有一个问题我没搞清楚,问题注在代码中. 题目大意: 外面下雨了,农民Johnoson的bull(?? ...