的property
decorator只适用于新型班;也就是说,从object
继承的类。获取(通过属性访问可以访问全局REQUEST
对象)是非常多的“old-skool”python,两者不能很好地协同工作,因为property
忽略了获取包装器,这是需要获取REQUEST
对象。
的Zope有它自己的property
式的方法,早新型类和property
decorater,叫ComputedAttribute
,这实际上通过多年早于property
装饰和新的样式类。但是,ComputedAttribute
包装的函数确实知道如何处理Acquisition
包装的对象。
您可以使用ComputedAttibute
很像property
装饰:
from ComputedAttribute import ComputedAttribute
class SomeClass():
@ComputedAttribute
def someProperty(self):
return 'somevalue'
的ComputedAttribute
包装函数也可以用包装的水平,这是我们需要采集的包装打交道时进行配置。实用模块中
from ComputedAttribute import ComputedAttribute
def computed_attribute_decorator(level=0):
def computed_attribute_wrapper(func):
return ComputedAttribute(func, level)
return computed_attribute_wrapper
棒这个地方:你不能使用ComputedAttribute
作为装饰在这种情况下:
class SomeClass():
def someValue(self):
return self.REQUEST
someValue = ComputedAttribute(someValue, 1)
这是很容易定义一个新的功能做装饰为我们虽然,之后,你就可以使用它作为一个可调用的装饰,以纪念的东西作为采集感知特性:
class SomeClass():
@computed_attribute_decorator(level=1)
def someValue(self):
return self.REQUEST
注意,不像property
,ComputedAttribute
只能用于吸气剂;不支持setter或deleters。
您是否使用诸如Persistence或Acquisition之类的基类? –
我有ObjectManager作为其中一个基类(从Persistent继承) – Rastaf
以及Acquisition.Implicit。 :-) –