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' #最佳实践 """ 为了避免前面所有的 ...
随机推荐
- vue数据已渲染成 但还是报错 变量 undefined
问题:页面上的数据已渲染出来,但是控制台还是报错变量未undefined,主要是当页面加载完成后,数据并未加载完,所以会报次错误. 解决办法:在数据渲染的主节点(最外层的div)添加 v-if=“da ...
- C#机器学习插件 ---- AForge.NET
目录 简介 主要架构 特点 学习之旅 简介 AForge.NET是一个专门为开发者和研究者基于C#框架设计的,这个框架提供了不同的类库和关于类库的资源,还有很多应用程序例子,包括计算机视觉与人工智能, ...
- c++重在运算符前置自增和后置自增
class student { int age; }; int main() { class student stu; (stu++)++;//error ++(stu++);//error stu+ ...
- PyBrain库的example之NFQ流程图分析
PyBrain库的example之NFQ流程图分析 如下是测试程序.主要分析doEpisode和learn两个函数. #!/usr/bin/env python __author__ = 'Thoma ...
- python3之es+log+date+timezone
from dateutil.parser import parse # 使用它可以方便的将字符串解析为datetimefrom tzlocal import get_localzone # 使用它可以 ...
- PTA 1005 Spell It Right (20)(20 分)水题
1005 Spell It Right (20)(20 分) Given a non-negative integer N, your task is to compute the sum of al ...
- Lock分析
Lock接口是锁的实现,用来控制多个线程访问共享资源的方式,是在java 1.5的时候引入的,在此之前,只能通过synchronized的方式来取得对象的锁. synchronized中的锁是隐式 ...
- sql sever数据库常用的执行语句
--使用master数据库use master --创建数据库文件create database 数据库名字 on( name=, --逻辑名称 filename= .ndf, --数据文件物理路径名 ...
- IDA Pro 权威指南学习笔记(十二) - IDA 中的注释
注释有助于以一种更高级的方式描述汇编语言指令序列 IDA 提供了几种不同类型的注释,每种注释适用于不同的目的 使用 Edit -> Comments 命令的选项,可以为反汇编代码清单中的任何一行 ...
- Java8函数式接口和Lambda表达式
两者关系: Lambda表达式就是函数式接口(FunctionalInterface)实现的快捷方式,它相当于函数式接口实现的实例,因为在方法中可以使用Object作为参数,所以把Lambda表达式作 ...