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' #最佳实践 """ 为了避免前面所有的 ...
随机推荐
- lib包含# #pragma comment
#pragma comment(lib,"d2d1.lib")#pragma comment(lib,"dwrite.lib")#pragma comment( ...
- pandas 之 set_index
set_index 很有用 http://stackoverflow.com/questions/10457584/redefining-the-index-in-a-pandas-dataframe ...
- ORACLE11g 没有控制文件如何通过rman备份恢复数据的详细实战过程
1.副总裁需要裸恢复的严峻现实 集团总部的信息部负责人给我打电话说为了找一年前的记录,所以需要对一年前2015年5月1日的数据进行恢复.而2016年初因为进行迁移,所以有些文件可能丢失,手上只有rma ...
- Unit02: Servlet工作原理
Unit02: Servlet工作原理 点击注册按钮,返回注册信息 package web; import java.io.IOException; import java.io.PrintWrite ...
- SQLServer中求两个字符串的交集(字符串以符号分隔)
两个字符串,以特定符号分隔(例如‘,’号),求交集 第一种情况: declare @m varchar(100),@n varchar(100)select @m=',2,3,5,7,8,9,10,' ...
- Windows下搭建Nginx图片服务器
在项目最开始,上传图片的时候,服务器先保存原图再使用ImageMagick生成上传图片缩略图,这种方法有很多缺点,例如生成的缩略图的大小是固定的,不能动态请求指定大小的缩略图. 虽然有非常多的图片云存 ...
- Java报错 -- The public type c must be defined in its own file
出现The public type c must be defined in its own file这个问题,是由于定义的JAVA类同文件名不一致 你的文件里很可能有两个 public 的类,而Ja ...
- 手势GestureDetector.OnGestureListener事件的调起
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionE ...
- node的express中间件之directory
direcotry中间件用于在浏览器中流出网站某个目录下的所有子目录及文件. app.use(express.directory(path,[options])); 查看网站根目录下的文件及目录 va ...
- 浅谈PHP面向对象编程(四、类常量和静态成员)
4.0 类常量和静态成员 通过上几篇博客我们了解到,类在实例化对象时,该对象中的成员只被当前对象所有.如果希望在类中定义的成员被所有实例共享. 此时可以使用类常量或静态成员来实现,接下来将针对类常量和 ...