实现描述符接口的其他装饰器
Dickens的Python项目详细描述
实现描述符接口的其他python装饰器。
用例
与内置的decoratorproperty一样,这些类通常由函数初始化并包装在类的上下文中,以便修改其行为。
缓存属性
这个decorator的功能非常类似于一个只读的property,其区别在于,在访问时,它将其结果记录在实例的对象数据字典中,该字典在查找时优先引用,从而替换该对象的自身:
from descriptors import cachedproperty @cachedproperty def circumference(self): return 2 * math.pi * self.radius
类属性
类方法的只读property:
from descriptors import classproperty @classproperty def badpi(cls): return 22 / 7
缓存类属性
一个类property,它将其结果缓存在调用它的类的数据字典中(使用另一个名称,以免影响属性的继承):
from descriptors import cachedclassproperty @cachedclassproperty def badpi(cls): return 22 / 7
安装
狄更斯是一个python发行版,可以通过easy_install或pip,安装,例如:
pip install Dickens
…或者,来源:
python setup.py install