[Python-Dev] Oberver Pattern

Raymond Hettinger python@rcn.com
Fri, 10 May 2002 19:15:42 -0400


From: "Guido van Rossum" <guido@python.org>
> Again, I think you are being just a dat naive here -- all those jumps
> (that are nearly always taken!) add up, and you're proposing to add
> this to every mutable object.

How about a simpler alternative?

Have just a single field in PyObject_HEAD, int cache_valid.  
Update routines wouldn't even need a jump.  
Everywhere the object assigns a value to ob_item[i], it would also run:  
PyObject_INVALIDATE_CACHE which would expand to:  
   self->cache_valid=0.

The only services would be macros for PyObject_IS_CACHE_VALID(obj)
and PyObject_VALIDATE_CACHE(obj).

> If you *have* to do this, I would implement (at the C level) just a
> single observer object.  If multiple observers are needed, this can
> easily be done using an adapter in Python.

Will do.  Since it's not that hard to implement (meaning, less time than
I spent on iterzip), I may as well try it and time it.


Raymond Hettinge