property
デコレーターは、新しいスタイルのクラスでのみ機能します。つまり、 を継承するクラスですobject
。一方、取得(属性アクセスを介してグローバルREQUEST
オブジェクトにアクセスできます)は非常に「古い」pythonであり、オブジェクトを取得するために必要な取得ラッパーをproperty
無視するため、2つはうまく連携しません。REQUEST
Zope には独自の のproperty
ようなメソッドがあり、これは新しいスタイルのクラスよりも前のものであり、実際にはデコレータと新しいスタイルのクラスより何年も前のproperty
と呼ばれるデコレータです。ただし、ラップされた関数は、ラップされたオブジェクトでどのように動作するかを知っています。ComputedAttribute
property
ComputedAttribute
Acquisition
デコレータ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
これをユーティリティ モジュールのどこかに貼り付けます。その後、呼び出し可能なデコレータとして使用して、何かを Acquisition-aware プロパティとしてマークすることができます。
class SomeClass():
@computed_attribute_decorator(level=1)
def someValue(self):
return self.REQUEST
とは異なりproperty
、ComputedAttribute
getter にのみ使用できることに注意してください。セッターまたはデリーターはサポートされていません。