[Zope] Zope (X)3 FAQ

Thomas Guettler Thomas Guettler <thomas@thomas-guettler.de>
Sun, 6 Apr 2003 22:59:28 +0200


On Sat, Apr 05, 2003 at 04:05:19PM +0200, Steve Alexander wrote:
> 
> >Will there be a "standard" way of updating existing instances to
> >changes in the python source?
> 
> You can use the __setstate__ hook for this.

I know. __setstate__ is what I called "implicit update". But AFAIK
objects which are already in memory don't get updated until the next
time they get unpickeled. That's why I prefere a explicit update. I
called this method refresh() in my python product.

> Ok. Can you explain exactly what your refresh() method does?

Example from zope mailinglist:

Imaginge you have a class Foo, and some instances are already in the
ZODB. Now you want to add a birthday.

class Foo:
    def __init__(self):
        self.birthday=time.time()

==> Only new created objects get a birthday. Old ones don't have this
attribute. You could do this:

#Example (1)
class Foo:
   birthday=0

But sometimes it is better to have a method which "refreshes" exiting
objects to changes in the python source.

#Example (2)
class Foo:
    def refresh(self):
        if not hasattr(self, "birthday"):
            self.birthday=0

Then your application can have a method like refreshAllObjects(),
which calls refresh on all objects. I know this does not scale if you
have several thousand objects already in the ZODB, but it works for my
solution.

This example could be solved with the code from example 1, but
sometimes you have more complex update logic which, I think, needs to
be done in a method like in example 2.

 Regards,
  Thomas

-- 
Thomas Guettler <guettli@thomas-guettler.de>
http://www.thomas-guettler.de