`
rstevens
  • 浏览: 94524 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

python学习: python中的descriptor

阅读更多
descriptor 用与类的属性

descriptor 是一个类,需要提供 __set__, __get__, __del__ 三个方法的实现。

如果一个类的属性是 descriptor,那么:

该属性不会出现在该类实例化后的对象的__dict__中。也就是说,它部作为字典的一部分出现。
访问该属性时,调用descriptor的__get__()方法
设置该属性时,调用descriptor的__set__()方法
删除该属性时,调用descriptor的__del__()方法

也就是说,如果属性是descriptor,则劫持了对字典的访问。

descriptor 的好处是什么?
增加了一层以后,可以提供更多的灵活性,感觉
分享到:
评论

相关推荐

    python学习-20-descriptor

    简单易学:Python的语法简洁明了,易于理解和学习。 高级特性:Python支持面向对象编程、函数式编程等高级特性,使得代码更加模块化和可重用。 广泛的库和框架:Python拥有丰富的第三方库和框架,可以快速开发各种...

    cron-descriptor:一个将cron表达式转换为人类可读字符串的Python库

    Cron描述符 ...使用画中画 pip install cron-descriptor 用法示例 # Simple from cron_descriptor import get_description , ExpressionDescriptor print ( get_description ( "* 2 3 * *" )) #OR prin

    Python中的Descriptor描述符学习教程

    下面先介绍一下Python中成员变量的定义和使用。 我们知道,在Python中定义类成员和C/C++相比得到的结果具有很大的差别。如下面的定义: class Cclass { int I; void func(); }; Cclass c; 在上面的定义中,...

    轻松理解Python 中的 descriptor

    定义 通常,一个 descriptor 是具有“绑定行为”的对象属性。所绑定行为可通过 descriptor 协议被自定义的 __get__() , __set__() ...如果查找链中,对象被定义了 descriptor 方法,Python 就会覆盖默认行为。 Descrip

    Python库 | person_descriptor-0.1.0.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:person_descriptor-0.1.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python描述符descriptor使用原理解析

    主要介绍了Python 描述符descriptor使用原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Python 的描述符 descriptor详解

    Python 在 2.2 版本中引入了descriptor(描述符)功能,也正是基于这个功能实现了新式类(new-styel class)的对象模型,同时解决了之前版本中经典类 (classic class) 系统中出现的多重继承中的 MRO(Method Resolution...

    详解Python中的Descriptor描述符类

    里我们将来详解Python中的Descriptor描述符类,包括定义描述符并展示如何调用描述符,需要的朋友可以参考下

    python3.6.5参考手册 chm

    PEP 487: Descriptor Protocol Enhancements PEP 519: Adding a file system path protocol PEP 495: Local Time Disambiguation PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change ...

    Python黑魔法Descriptor描述符的实例解析

    在Python中,访问一个属性的优先级顺序按照如下顺序: 1:类属性 2:数据描述符 3:实例属性 4:非数据描述符 5:__getattr__()方法 这个方法的完整定义如下所示: def __getattr(self,attr) :#attr是self的一个属性名 ...

    Python中的descriptor描述器简明使用指南

    descriptor在Python中主要被用来定义方法和属性,使用起来相当具有技巧性,这里我们先从基础的开始,整理一份Python中的descriptor描述器简明使用指南

    Python & Matlab code for local feature descriptor evaluation wit

    Python & Matlab code for local feature descriptor evaluation with the HPatches dataset.zip

    python-3.6-book

    howto-descriptor.pdf howto-functional.pdf howto-instrumentation.pdf howto-ipaddress.pdf howto-logging.pdf howto-logging-cookbook.pdf howto-pyporting.pdf howto-regex.pdf howto-sockets.pdf howto-sorting...

    Python描述器descriptor详解

    主要向我们详细介绍了Python描述器descriptor,需要的朋友可以参考下

    深入解析Python中的descriptor描述器的作用及用法

    在Python中描述器也被称为描述符,描述器能够实现对对象属性的访问控制,下面我们就来深入解析Python中的descriptor描述器的作用及用法

    Python.Descriptors

    This short book on Python descriptors is a collection of knowledge and ideas from many sources on dealing with and creating descriptors. And, after going through the things all descriptors have in ...

    解密Python中的描述符(descriptor)

    Python中包含了许多内建的语言特性,它们使得代码简洁且易于理解。这些特性包括列表/集合/字典推导式,属性(property)、以及装饰器(decorator)。对于大部分特性来说,这些“中级”的语言特性有着完善的文档,...

Global site tag (gtag.js) - Google Analytics