该property
装饰只适用于新的风格 类; 也就是说,继承自的类object
。REQUEST
另一方面,获取(通过属性访问使您可以访问全局对象)是非常“老套”的python,两者不能很好地协同工作,因为property
忽略 了获取REQUEST
对象所需的获取包装器。
Zope有自己的property
类似方法,可以在新型类之前进行修饰,而装饰器property
称为ComputedAttribute
,实际上可以在property
装饰器和新类上进行许多年的修饰。但是,ComputedAttribute
-wrapped函数确实知道如何与Acquisition
-wrapped对象一起工作。
您可以ComputedAttibute
像property
装饰器一样使用:
from ComputedAttribute import ComputedAttribute
class SomeClass():
@ComputedAttribute
def someProperty(self):
return 'somevalue'
该ComputedAttribute
包装函数也可以用包装的水平,这是我们需要采集的包装打交道时进行配置。ComputedAttribute
在这种情况下,您不能将用作装饰器:
class SomeClass():
def someValue(self):
return self.REQUEST
someValue = ComputedAttribute(someValue, 1)
from ComputedAttribute import ComputedAttribute
def computed_attribute_decorator(level=0):
def computed_attribute_wrapper(func):
return ComputedAttribute(func, level)
return computed_attribute_wrapper
将其放在实用程序模块中的某个位置,然后可以将其用作可调用的修饰符,以将某些内容标记为“采集感知”属性:
class SomeClass():
@computed_attribute_decorator(level=1)
def someValue(self):
return self.REQUEST