overloading getattr on a product
Hello, 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". 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. -Chris
Christopher N. Deckard wrote:
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.
Hmmm, maybe it tries a object[index] if that fails?
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 have a self somewhere in you __getattr__ which you don't meant to be there ;-) In general, overloading __getattr__ in the presence of acquisition is akin to putting a fluffy kitten on a railway track when you don't know what times the trains run... cheers, Chris
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
participants (3)
-
Chris Withers -
Christopher N. Deckard -
Dieter Maurer