Python Zope:无法访问属性装饰器下的请求

Python Zope:无法访问属性装饰器下的请求,python,properties,request,zope,Python,Properties,Request,Zope,我试图在类中使用属性装饰器。虽然它本身运行良好,但我不能使用任何必须访问请求的代码 class SomeClass(): #Zope magic code _properties=({'id':'someValue', 'type':'ustring', 'mode':'r'},) def get_someValue(self): return self.REQUEST @property def someValue(self): return sel

我试图在类中使用属性装饰器。虽然它本身运行良好,但我不能使用任何必须访问
请求的代码

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
尽管调用
get_someValue
会得到所需的结果,但尝试访问
someValue
会引发
AttributeError

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

(我使用的是Zope2.13.16,Python2.7.3)

它只适用于新样式的类;也就是说,继承自
对象的类。另一方面,Acquisition(它允许您通过属性访问访问全局
请求
对象)是非常“旧的skool”python,两者不能很好地协同工作,因为
属性
忽略了获取
请求
对象所需的采集包装器

Zope有自己的
property
类方法,它比新样式类和
property
decorator(称为)更早,实际上比
property
decorator和新样式类早很多年。不过,
ComputedAttribute
-wrapped函数确实知道如何处理
采集
-wrapped对象

您可以像
属性
装饰器一样使用
ComputedAttibute

from ComputedAttribute import ComputedAttribute

class SomeClass():   
    @ComputedAttribute
    def someProperty(self):
        return 'somevalue'
ComputedAttribute
wrapper函数也可以配置一定级别的包装,这是我们在处理采集包装时所需要的。在这种情况下,不能将
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
注意,与
属性
不同,
计算属性
只能用于getter;不支持setter或deleter。

仅适用于新样式的类;也就是说,继承自
对象的类。另一方面,Acquisition(它允许您通过属性访问访问全局
请求
对象)是非常“旧的skool”python,两者不能很好地协同工作,因为
属性
忽略了获取
请求
对象所需的采集包装器

Zope有自己的
property
类方法,它比新样式类和
property
decorator(称为)更早,实际上比
property
decorator和新样式类早很多年。不过,
ComputedAttribute
-wrapped函数确实知道如何处理
采集
-wrapped对象

您可以像
属性
装饰器一样使用
ComputedAttibute

from ComputedAttribute import ComputedAttribute

class SomeClass():   
    @ComputedAttribute
    def someProperty(self):
        return 'somevalue'
ComputedAttribute
wrapper函数也可以配置一定级别的包装,这是我们在处理采集包装时所需要的。在这种情况下,不能将
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


注意,与
属性
不同,
计算属性
只能用于getter;不支持setter或deleter。

如果您想要绕过需要获取的路径,并且无法从类的构造函数中调用代码显式设置请求,请使用zope.globalrequest。否则,您可能需要考虑一个浏览器视图(它总是适应一些上下文和请求)。

< P>如果您想绕过需要获取,并且不能显式地将请求从类的构造函数中设置为调用代码,请使用Zop.GuulalRevices。否则,您可能需要考虑一个浏览器视图(它总是适应一些上下文和请求)。

您使用基类,如持久性或获取吗?我有ObjtMe饰的作为基类之一(继承自持久性)以及获取。隐式。您是否使用诸如Persistence或Acquisition之类的基类?我将ObjectManager作为基类之一(继承自Persistent)以及Acquisition.Implicit.:-)非常感谢你!我已经使用Zope好几年了,从来没有在ComputedAttribute方法上遇到过问题。在一个例子中(Plone/Dextrity内容项),这似乎对我有效,但为了澄清一下:在新样式的类上使用基于ComputedAttribute的装饰器是否有任何已知的副作用?类必须扩展哪些基类才能使用ComputedAttribute(例如,在参与采集的浏览器视图中)?您只能对从
ExtensionClass
派生的类使用ComputedAttribute(其中包括
acquisition.Explicit
acquisition.Implicit
类)。除此之外,没有其他要求。我不知道新样式类的其他特殊注意事项。值得注意的是,对于ExtensionClass 4.2.1+,ComputedAttribute使用的包装级别可能需要更改。为了以上述方式使用装饰器,我必须将level=1更改为level=0。当升级到Plone 5.1+时,这可能特别适用于Plone用户在其加载项中使用ComputedAttribute。非常感谢!我已经使用Zope好几年了,从来没有在ComputedAttribute方法上遇到过问题。在一个例子中(Plone/Dextrity内容项),这似乎对我有效,但为了澄清一下:在新样式的类上使用基于ComputedAttribute的装饰器是否有任何已知的副作用?类必须扩展哪些基类才能使用ComputedAttribute(例如,在参与acq的浏览器视图中)