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' #最佳实践 """ 为了避免前面所有的 ...
随机推荐
- BZOJ1556 墓地秘密
题意 费尽周折,终于将众将士的残骸运送到了KD军事基地地底层的大型墓地入口.KD的伙伴和战友们都参加了这次重大的送葬仪式.右边是一扇敞开的大门,进去便是墓地了,左边是一堵凹进去的墙,没有什么特别的地方 ...
- MySQL Geometry扩展在地理位置计算中的效率优势
由于在Geometry中,有相关自带函数和SPATIAL INDEX的性能优化,可以让某些位置计算的效率提升.以下是几种计算方法的效果对比. 1. 数据准备 首先创建一个数据表,这是一个店铺数据表,结 ...
- (转)Tomcat迁移JBoss杂症—不识别及不能解析web.xml
本文介绍了在将tomcat下的web工程迁移到jboss下面时遇到的问题 背景: Tomcat 7.0 JBoss AS 4.2.2 IED: Eclipse Java EE IDE for Web ...
- Web 漏洞分析与防御之点击劫持(三)
原文地址:Web 漏洞分析与防御之点击劫持(三) 博客地址:http://www.extlight.com 一.全称 点击劫持,顾名思义,用户点击某个按钮,却触发了不是用户真正意愿的事件. 二.原理 ...
- Python2 和 Python3 的区别(待完善)
1.宏观上 python2 :源码不标准,混乱,重复代码太多 python3 :统一 标准,去除重复代码. 2. print python2 :括号可有可无 print(a) 或 print ap ...
- windows7下怎样安装whl文件(python)
本文转载自:http://blog.csdn.net/fhl812432059/article/details/51745226 windows7 python2.7 1.用管理员方式打开cmd 2. ...
- s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由用户控制。
package com.hanqi; import java.util.*; public class yonghukongzhi { public static void main(String[] ...
- temple-html5
ylbtech-HTML5: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 1 ...
- Nginx 之:nginx.conf结构
nginx.conf文件结构如下: ..... events { ...... } http{ .... server{ .... } server{ listen 192.168.0.12:80; ...
- 性能监控之Spotlight
有和同事探讨一些技术监控类软件,谈到Spotlight,临时记一下. 下载地址https://pan.baidu.com/s/1HB9xd9LmOR-MOk8FIGBm-A 常规安装模式,下载解压安装 ...