[ZODB-Dev] Computed Attributes and property's

Christian Heimes christian at cheimes.de
Tue May 17 16:53:52 EDT 2005


Stephen Masterman wrote:
> Does ZODB have something equivalent to Durus' Computed Attributes? Which
> I understand are attributes (or methods) on a Persistent object that
> compute a value from some other attributes and then cache that result
> for each connection, and if any of the attributes from which the
> Computed Attribute computes its result change, the new result will be
> automatically computed and all connections will have their cached value
> invalidated.

Zope 2 has ComputedAttribute. It allows you to access the result of a 
method as an attribute:

from ComputedAttribute import ComputedAttribute
import ExtensionClass

class Example(ExtensionClass.Base):

     def _anAttribute(self):
         return 'foo'

     anAttribute = ComputedAttribute(_anAttribute)

 >>> example = Example()
 >>> example.anAttribute
'foo'
 >>>


> On a related note, do properties work with Persistent objects in ZODB? I
> mean the Python property() function. I do not know how it interacts with
> __getattr__() and stuff, and I know the ZODB docs warn against messing
> with those functions for Persistent classes. If they do work, do they
> basically serve as a non-cached computed attribute? Is a Durus Computed
> Attribute just a property with caching and consistency for multiple
> connections? (I know this isn't a Durus list.)

For your information:
property() doesn't work for ExtensionClass bases classes. You can't use 
them in Zope 2 up to 2.7. Python properties are working with ZODB 3.3+ 
(Zope 2.8 and Zope3).


Christian


More information about the ZODB-Dev mailing list