Zope:无法访问属性装饰器下的REQUEST

Ras*_*taf 6 python zope properties request

我正在尝试在类中使用属​​性装饰器.虽然它本身很好用,但我不能使用任何必须访问的代码REQUEST.

class SomeClass():
   #Zope magic code
   _properties=({'id':'someValue', 'type':'ustring', 'mode':'r'},)

  def get_someValue(self):
    return self.REQUEST

  @property
  def someValue(self):
    return self.REQUEST
Run Code Online (Sandbox Code Playgroud)

虽然打电话get_someValue让我得到了理想的结果,但试图访问someValue会引发一个问题AttributeError.

这种行为背后的逻辑是什么?有没有办法解决这个限制?

(我使用的是Zope 2.13.16,Python 2.7.3)

Mar*_*ers 6

property装饰只适用于新的风格类; 也就是说,继承自的类object.REQUEST另一方面,获取(通过属性访问使您可以访问全局对象)是非常"老式"的python,并且两者不能很好地协同工作,因为property 忽略了获取REQUEST对象所需的获取包装器.

Zope拥有自己property喜欢的方法,可以预先设定新式的类和property装饰器ComputedAttribute,这种方法实际上早在property装饰师和新式课程之前就已经存在多年了.但是,ComputedAttribute-wrapped函数确实知道如何使用被Acquisition包装的对象进行操作.

你可以ComputedAttibuteproperty装饰一样使用:

from ComputedAttribute import ComputedAttribute

class SomeClass():   
    @ComputedAttribute
    def someProperty(self):
        return 'somevalue'
Run Code Online (Sandbox Code Playgroud)

ComputedAttribute包装函数也可以用包装的水平,这是我们需要采集的包装打交道时进行配置.ComputedAttribute在这种情况下,你不能使用它作为装饰器:

class SomeClass():   
    def someValue(self):
        return self.REQUEST
    someValue = ComputedAttribute(someValue, 1)
Run Code Online (Sandbox Code Playgroud)

虽然很容易定义一个新功能来为我们做装饰:

from ComputedAttribute import ComputedAttribute

def computed_attribute_decorator(level=0):
    def computed_attribute_wrapper(func):
        return ComputedAttribute(func, level)
    return computed_attribute_wrapper
Run Code Online (Sandbox Code Playgroud)

将其粘贴在某个实用程序模块中,之后您可以将其用作可调用的装饰器,将某些内容标记为可识别的属性:

class SomeClass(): 
    @computed_attribute_decorator(level=1)
    def someValue(self):
        return self.REQUEST
Run Code Online (Sandbox Code Playgroud)

注意,不像property,ComputedAttribute只能用于吸气剂; 不支持制定者或删除者.