[Zope] overloading getattr on a product
Dieter Maurer
dieter@handshake.de
Sat, 17 Aug 2002 11:47:31 +0200
Christopher N. Deckard writes:
> I'm writing a product which stores the equivalent
> of Folder properties in XML. Now I want to be
> able to catalog objects of this type, but looking
> at how that's done by the ZCatalog shows that
> getattr(object, index) is called for all indexes.
>
> How do I overload getattr to parse my XML and
> return the value of the "property".
This sounds very expensive...
> My product is subclassed from ParsedXML and uses
> the DOM to go through the tree and find my
> "properties". What seems to be happening is that
> when I define a new __getattr__, implicit getattr
> calls are made to the DOM object and it ends up
> calling my newly defined getattr method. This
> sort of send it into a recursive neverending loop.
You can find a similar problem ("Something wrong with METAL")
in the ZPT mailing list archives. I made a similar error to that
you do.
It is very easy to create "__getattr__" loops.
You must make sure that every attribute reference in
your "__getattr__" is defined.
My error was that I used "return getattr(self.attr,key)"
where "attr" was a (defined) attribute from
a class derived by "Acquisition.Implicit".
Instead of raising an "AttributeError" when "attr" did not
define "key", it ask "self" (--> recursive loop).
Dieter