开始看官方文档,各种看不懂,只看到一句Properties, bound and unbound methods, static methods, and class methods are all based on the descriptor protocol. 然后看了一篇很不错的文章Python描述符(descriptor)解密,至少让我知道为什么要描述符,以及描述符能做什么.简言之,描述符可以简化重复的property逻辑. 下面是转载内容: Python中包含了许多内建的语言特性,它…
Python中的描述符是一个相对底层的概念 descriptor Any object which defines the methods get(), set(), or delete(). When a class attribute is a descriptor, its special binding behavior is triggered upon attribute lookup. Normally, using a.b to get, set or delete an att…
Python的描述符乍眼看去简单,但是细节方面如果不注意容易掉坑,总结以下几个坑,以作备忘,先看代码: class D: def __get__(self, inst, owner): if inst is None: return self else: print('I am in the D.__get__') return inst.__dict__['d'] # 返回实例的d属性 def __set__(self, inst, value): if not isinstance(valu…