描述符用法建议,

内置的 property 类创建的是'覆盖型'(date descriptor), 实现了 __set__ 和 __get__.
特性 property 的 __set__ 方法 默认会抛出 'AttributeError: can not set attribute',
也就是说使用 property 是创建'只读'属性的一个简单的方式。 然而, 如果通过 descriptor 来实现'只读' 属性, 一定要 同时实现 __set__ 和 __get__ 方法,
否则, 实例的同名属性会遮盖 descriptor. 用于验证数据的 descriptor 可以只有 __set__ 方法 来 验证数据的有效性.
如果有效, 使用 'descriptor 实例' 的名称为 '键', 在'实例' 的 __dict__ 属性中设置。
从'实例'中读取同名属性的速度更快, 因为不需要经过 __get__ 方法处理. 如果只实现了 __get__ 方法, 是'非覆盖型'(non-data) descriptor. 这类描述符适用于执行一些耗费资源的计算,
然后向'实例'设置同名属性, 以缓存结果. 同名的'实例属性'会遮盖 descriptor, 因此, 后面再访问会直接从实例的
__dict__ 属性中的到值, 不会触发描述符的 __get__ 方法. Invoking descriptors,
In general, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been
overridden by methods in the descriptor protocol: __get__(), __set__(), and __delete__().
If any of those methods are defined for an object, it is said to be a descriptor. The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary.
For instance, a.x has a lookup chain starting with a.__dict__['x'], then type(a).__dict__['x'],
and continuing through the base classes of type(a) excluding metaclasses. However, 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 and how they were called. The starting point for descriptor invocation is a binding, a.x. How the arguments are assembled depends on a: Direct Call
The simplest and least common call is when user code directly invokes a descriptor method: x.__get__(a). Instance Binding
If binding to an object instance, a.x is transformed into the call: type(a).__dict__['x'].__get__(a, type(a)). Class Binding
If binding to a class, A.x is transformed into the call: A.__dict__['x'].__get__(None, A). Super Binding
If a is an instance of super, then the binding super(B, obj).m() searches obj.__class__.__mro__ for the base
class A immediately preceding B and then invokes the descriptor with the call: A.__dict__['m'].__get__(obj, obj.__class__). For instance bindings, the precedence of descriptor invocation depends on the which descriptor methods are defined.
A descriptor can define any combination of __get__(), __set__() and __delete__(). If it does not define __get__(),
then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary.
If the descriptor defines __set__() and/or __delete__(), it is a data descriptor; if it defines neither, it is a non-data descriptor.
Normally, data descriptors define both __get__() and __set__(), while non-data descriptors have just the __get__() method.
Data descriptors with __set__() and __get__() defined always override a redefinition in an instance dictionary.
In contrast, non-data descriptors can be overridden by instances. Python methods (including staticmethod() and classmethod()) are implemented as non-data descriptors.
Accordingly, instances can redefine and override methods. This allows individual instances to acquire
behaviors that differ from other instances of the same class. The property() function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property.

Invoking Descriptors - Python 描述符的用法建议的更多相关文章

  1. 杂项之python描述符协议

    杂项之python描述符协议 本节内容 由来 描述符协议概念 类的静态方法及类方法实现原理 类作为装饰器使用 1. 由来 闲来无事去看了看django中的内置分页方法,发现里面用到了类作为装饰器来使用 ...

  2. python描述符 descriptor

    descriptor 在python中,如果一个新式类定义了__get__, __set__, __delete__方法中的一个或者多个,那么称之为descriptor.descriptor通常用来改 ...

  3. Descriptor - Python 描述符协议

    描述符(descriptor) descriptor 是一个实现了 __get__. __set__ 和 __delete__ 特殊方法中的一个或多个的. 与 descriptor 有关的几个名词解释 ...

  4. python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解

     1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...

  5. 【转载】Python 描述符简介

    来源:Alex Starostin 链接:www.ibm.com/developerworks/cn/opensource/os-pythondescriptors/ 关于Python@修饰符的文章可 ...

  6. python描述符descriptor(一)

    Python 描述符是一种创建托管属性的方法.每当一个属性被查询时,一个动作就会发生.这个动作默认是get,set或者delete.不过,有时候某个应用可能会有 更多的需求,需要你设计一些更复杂的动作 ...

  7. [转载]ACM(访问控制模型),Security Identifiers(SID),Security Descriptors(安全描述符),ACL(访问控制列表),Access Tokens(访问令牌)

    对于<windows核心编程>中的只言片语无法驱散心中的疑惑.就让MSDN中的解释给我们一盏明灯吧.如果要很详细的介绍,还是到MSDN仔细的看吧,我只是大体用容易理解的语言描述一下. wi ...

  8. Python描述符的使用

    Python描述符的使用 前言 作为一位python的使用者,你可能使用python有一段时间了,但是对于python中的描述符却未必使用过,接下来是对描述符使用的介绍 场景介绍 为了引入描述符的使用 ...

  9. Python描述符 (descriptor) 详解

    1.什么是描述符? python描述符是一个“绑定行为”的对象属性,在描述符协议中,它可以通过方法重写属性的访问.这些方法有 __get__(), __set__(), 和__delete__().如 ...

随机推荐

  1. swiper如何禁止左右箭头切换

    swiper做项目时,需求 带着左右两边的箭头, 场景1:swiper自动切换,此时左右箭头点击时不能切换   场景2:swiper手动切换,左右箭头可以实现切换,通过翻阅api 终于找到 <s ...

  2. 更好用的 Python 任务自动化工具:nox 官方教程

    英文| nox tutorial 出处| nox 官方文档 译者| 豌豆花下猫@Python猫 Github地址:https://github.com/chinesehuazhou/nox_doc_c ...

  3. JSP九大内置对象之session以及eclispe如何关联源码

    一.session的特点及其实例 session:同一次会话共享 a.浏览网站:开始->关闭 b.购物:浏览.付款.退出 c.电子邮件:浏览.写邮件.退出  从一次开始到一次结束,是一次会话.  ...

  4. CTO说|非容器化应用怎么玩多云?Kubernetes不管我们管啊

    Kubernetes已经成为容器编排系统的事实标准,是现在主流的跨云容器化应用操作系统. 但是,Kubernetes的目标并不是容器本身,而是承载其上的应用,本质上是为了解决(容器化)应用上云这个难题 ...

  5. Webpack实战(六):如何优雅地运用样式CSS预处理

    上一篇文章中,我主要分享了<Webpack如何分离样式文件>CSS 预处理器是一个能让你通过预处理器自己独有的语法来生成CSS的程序,css预处理指的是在开发中我们经常会使用一些样式预编译 ...

  6. [bzoj4824][洛谷P3757][Cqoi2017]老C的键盘

    Description 老 C 是个程序员. 作为一个优秀的程序员,老 C 拥有一个别具一格的键盘,据说这样可以大幅提升写程序的速度,还能让写出来的程序 在某种神奇力量的驱使之下跑得非常快.小 Q 也 ...

  7. EsClientRHL-elasticsearch java客户端开源工具

    EsClientRHL是一个可基于springboot的elasticsearch 客户端调用封装工具,通过elasticsearch官网推荐的RestHighLevelClient实现,内置了es索 ...

  8. C++ 引用分析

    引用 左值引用,建立既存对象的别名 右值引用,可用于为临时对象延长生命周期 转发引用,保持函数实参的类别 悬置引用,对象生命周期已经结束的引用,访问改引用为未定义行为 值类别,左值,纯右值,亡值 st ...

  9. UIKit, AppKit, 以及其他API在多线程当中的使用注意事项

    UIKit, AppKit, 以及其他API在多线程当中的使用注意事项 Overview The Main Thread Checker is a standalone tool for Swift ...

  10. Spring Boot定义系统启动任务的两种方式

    Spring Boot定义系统启动任务的两种方式 概述 如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web ...