[Zope-dev] Correct place to initialize _v_ attributes

Michel Pelletier michel@digicool.com
Fri, 12 Nov 1999 11:46:20 -0500


Stuart 'Zen' Bishop wrote:
> 
> Is there a method I can initialize _v_ attributes in? __init__ is
> only called on initial object creation, but I also need to initialize
> variables when the object is instantiated from ZODB

Define a __setstate__.  This is called when the object is activated
(brought into memory from the object store).

Here is an example of how Catalog wakes up:


    def __setstate__(self, state):
        """ initialize your brains.  This method is called when the
        catalog is first activated (from the persistent storage) """
        Persistent.__setstate__(self, state)
        self.useBrains(self._v_brains)
        if not hasattr(self, 'lexicon'):
            self.lexicon = Lexicon()


There are a couple things going on here, first, the Persistence
machinery is fired up (your object must subclass Persistent, of course),
second useBrains() initializes the default record class for catalog
result records.  third, 'self.lexicon' was added to Catalog in this beta
release, so everyone's old 2.0 Catalogs do not contains lexicons.  By
checking for the attr in setstate, you can evolve your objects to
contain new components and attributes.  You can also add a class
attribute, of course.  

-Michel


>  ___
>    //     Zen (alias Stuart Bishop)     Work: zen@cs.rmit.edu.au
>   // E N  Senior Systems Alchemist      Play: zen@shangri-la.dropbear.id.au
>  //__     Computer Science, RMIT         WWW: http://www.cs.rmit.edu.au/~zen
> 
> _______________________________________________
> Zope-Dev maillist  -  Zope-Dev@zope.org
> http://lists.zope.org/mailman/listinfo/zope-dev
>            No cross posts or HTML encoding!
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope )