[Zope-dev] ComputedAttribute, Persitent and "too many
datafull base classes"
Phillip J. Eby
pje@telecommunity.com
Sun, 21 Oct 2001 09:20:52 -0500
At 03:44 PM 10/20/01 +0200, Godefroid Chapelle wrote:
>Hi,
>
>In a product I am writing, I am trying to add a ComputedAttribute to an
>instance of a Folder descendant class.
>
>This is the code I use:
>
> def addComputedAttribute(self, attributeName, sourceCode):
> methodName = '_ca_get%s' % attributeName
> self.addMethod(methodName, sourceCode)
> setattr(self, attributeName,
> ContextComputedAttribute(self.__class__.__dict__[methodName]))
I think the problem you're having is that the method object being used in
the last line (i.e., self.__class__.__dict__[methodName]) is not
persistent. If it is a Python function, it will not be saved persistently
within the computed attribute. This is probably why the Zope instance
crashes at a later time.
This doesn't have anything to do with whether ComputedAttribute is
Persistent. It doesn't need to be, which is a good thing because you can't
derive a class from both (as you've discovered). You can write a Python
version of ComputedAttribute by deriving only from Persistent, but as I
said there is really no need to do that. You need the function to be
persistent, not the ComputedAttribute itself. If the function is currently
a pure-Python function, you need to replace it with some kind of callable
object that can be pickled, such as a PythonScript. You could also write
your own class to do this, but again it is not the ComputedAttribute part
that would cause a problem.