Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://docs.python.org/library/functions.html#property"><code>property</code> decorator</a> only works with <em>new-style</em> classes; that is to say, classes that inherit from <code>object</code>. Acquisition (which gives you access to the global <code>REQUEST</code> object via attribute access) on the other hand is very much 'old-skool' python and the two do not work well together, as <code>property</code> <em>ignores</em> acquisition wrappers, which are needed to acquire the <code>REQUEST</code> object.</p> <p>Zope has it's own <code>property</code>-like method that pre-dates new-style classes and the <code>property</code> decorater, called <a href="http://wiki.zope.org/zope2/ComputedAttribute"><code>ComputedAttribute</code></a>, which actually predates the <code>property</code> decorator and new-style classes by many years. A <code>ComputedAttribute</code>-wrapped function does know how to behave with an <code>Acquisition</code>-wrapped object, though.</p> <p>You can use <code>ComputedAttibute</code> much like the <code>property</code> decorator:</p> <pre><code>from ComputedAttribute import ComputedAttribute class SomeClass(): @ComputedAttribute def someProperty(self): return 'somevalue' </code></pre> <p>The <code>ComputedAttribute</code> wrapper function also can be configured with a level of wrapping, which is what we need when dealing with Acquisition wrappers. You cannot use the <code>ComputedAttribute</code> as a decorator in that case:</p> <pre><code>class SomeClass(): def someValue(self): return self.REQUEST someValue = ComputedAttribute(someValue, 1) </code></pre> <p>It is easy enough to define a new function to do the decorating for us though:</p> <pre><code>from ComputedAttribute import ComputedAttribute def computed_attribute_decorator(level=0): def computed_attribute_wrapper(func): return ComputedAttribute(func, level) return computed_attribute_wrapper </code></pre> <p>Stick this in a utility module somewhere, after which you can then use it as a callable decorator to mark something as an Acquisition-aware property:</p> <pre><code>class SomeClass(): @computed_attribute_decorator(level=1) def someValue(self): return self.REQUEST </code></pre> <p>Note that unlike <code>property</code>, <code>ComputedAttribute</code> can only be used for getters; there is no support for setters or deleters.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload