[Persistence-sig] Observing object changes

holger krekel pyth@devel.trillke.net
Wed, 14 Aug 2002 01:14:52 +0200


Lamy Jean-Baptiste wrote:
> I've just see the new persistance-SIG and i realize i have already worked
> on observing object changes. I use it for updating graphical interfaces
> when objects are changed, and for debugging, but it appears it can be
> usefull for OO database too -- so such a feature would rock !
> 
> 
> Here is the API i use:
> 
> addevent   (obj, func) -- Add    the event FUNC (=an observer) to OBJ
> removeevent(obj, func) -- Remove the event FUNC from OBJ
> 
> I have already written a pure Python implementation of that (feel free to
> ask for the code if you're interested); it works well but it is really an
> ugly hack ! The principle is to change the class of the observed object to
> a new class that define a __setattr__ method.

This is not enough.  Not every modifiction of an object goes through
'__setattr__', e.g.

    myobj.somelist.append(42)

would modify 'myobj' but you wouldn't notice. So everything an object
passes out (like its attribute) would need to have a thin wrapper which
holds a reference to "its" object and notifies upon change.  This is
likely to be a recursive process.

regards,

    holger