python's descriptor
【python's descriptor】
1、实现了以下三个方法任意一个的,且作为成员变量存在的对象,就是descriptor。
1)object.__get__(self, instance, owner):instance是实例的引用,owner是类对象的引用。
2)object.__set__(self, instance, value)
3)object.__delete__(self, instance)
The descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents
descriptor必须是类属性(class attribute)。
参考:http://docs.python.org/2.7/reference/datamodel.html?highlight=__mro__#implementing-descriptors
2、Invoking
上图红线框部分,可以看到a.x会被转换为 type(a).__dict__['x'].__get__(a, type(a))。这说明两个问题:
1、Class.X调用是无用的,因为会被转化为type(Class).__dict__['X'],type(Class)中显然没定义。
2、descriptor必须是类属性。
3、function & unbound method & bound method
这三个东西都是同一个对象,当def function时,一个function对象会实现__get__方法,so,一个function就是一个descriptor,根据__get__(self, inst, cls)中传入参数的不同,返回不同的输出。
4、property & super 也基于descriptor
参考:http://docs.python.org/2.7/reference/datamodel.html?highlight=__mro__#implementing-descriptors
参考:http://www.cnblogs.com/btchenguang/archive/2012/09/18/2690802.html
python's descriptor的更多相关文章
- python's descriptor II
[python's descriptor II] For instance, a.x has a lookup chain starting with a.__dict__['x'], then ty ...
- python中descriptor的应用
[python中descriptor的应用] 1.classmethod. 1)classmethod的应用. 2)classmethod原理. 2.staticmethod. 1)staticmet ...
- Python的descriptor (2)
前面说了descriptor,这个东西其实和Java的setter,getter有点像.但这个descriptor和上文中我们开始提到的函数方法这些东西有什么关系呢? 所有的函数都可以是descrip ...
- Python的Descriptor和Property混用
一句话,把Property和Descriptor作用在同一个名字上,就只有Property好使.
- python中的 descriptor
学好和用好python, descriptor是必须跨越过去的一个点,现在虽然Python书籍花样百出,但是似乎都是在介绍一些Python库而已,对Python语言本身的关注很少,或者即使关注了,但是 ...
- Python描写叙述符(descriptor)解密
Python中包括了很多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些" ...
- Python——描述符(descriptor)解密
本文由 极客范 - 慕容老匹夫 翻译自 Chris Beaumont.欢迎加入极客翻译小组,同我们一道翻译与分享.转载请参见文章末尾处的要求. Python中包含了许多内建的语言特性,它们使得代码简洁 ...
- 【python】类(资料+疑惑)
1.http://python-china.org/t/77 有关method binding的理解 2.[Python] dir() 与 __dict__,__slots__ 的区别 3.Descr ...
- python高级编程之最佳实践,描述符与属性01
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #最佳实践 """ 为了避免前面所有的 ...
随机推荐
- LeetCode Friend Circles
原题链接在这里:https://leetcode.com/problems/friend-circles/description/ 题目: There are N students in a clas ...
- 控制已经打开的Excel
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 记录一些WPF常用样式方便以后复用(二)(Button、CheckBox、输入账号密码框)(转)
Button (一) <Style x:Key="ButtonSaveStyle" TargetType="{x:Type Button}"> &l ...
- Fuel9.0安装openstack过程中所踩过的坑2018最新版
坑一,安装好后,无法访问Web UI画面 访问https//10.20.0.2:8443无法打开UI画面.首先我们不管以后的步骤,打不开是很不爽的. 解决方法:把下面网卡1,网卡2,网卡3的界面名称都 ...
- QT4.8.6静态编译
下载源安装程序,http://download.qt.io/archive/qt/4.8/4.8.6/qt-everywhere-opensource-src-4.8.6.tar.gz 解压 cd 进 ...
- callback&&callback()
如果存在回调函数就执行!这是利用了 JS &&符号的一个小技巧&& 符号在前面为假时就不会执行后面的语句了所以这个就相当于 if(callback){ callback ...
- GNU Radio: 自定义 block 实例
综述 本文通过在GNU Radio 中编写一个block的例子,系统介绍创建一个block的过程.该 block 的功能是可以在GRC中通过滑块(WX GUI Slider)来实时改变信号源(Sign ...
- python中高阶函数学习笔记
什么是高阶函数 变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数 def fun(x, y, f): print f(x), f(y) fun ...
- mybatis实现继承映射
ORM 框架的优势在于能让我们利用面向对象的思维去操作数据库, hibernate 作为重量级的 ORM 框架对面向对象的支持很强大.作为半自动化的 mybatis ,对面向对象的支持也是很完备的.这 ...
- ubuntu16.04编译QT5.6所依赖的库
首先在QT的根目录下,阅读README文件! 里面介绍了ubuntu环境下,编译该版本的QT需要安装的包 New dependencies in Qt 5 ------------------- ...