[Zope] overwrite __getattr__

Oliver Bleutgen myzope@gmx.net
Sat, 05 Apr 2003 15:27:36 +0200


Dragos Chirila wrote:
> I have a class that extends 'Folder' and in this class i have a property 
> of type dictionary.
> This dictionary has the following meaning: key = property id, value = a 
> value (string or whatever).
> I want to use the catalog to index and search for this kind of objects.
> For each property I have an index in catalog.
>  
> Let's say the dictionary is {'field1' : 'value of field 1', 'field2': 
> 'value of field2'}.
> I also have 2 indexes named 'field1' and 'field2'.
>  
> The problem is that when the catalog tries to catalog my object doesn't 
> find the properties 'field1' and 'field2' for my object !!!
> So, I overwrite __getattr__ to tell it how to handle these properties
>  
> def __getattr__(self, name):
>     if name=='field1':
>         return self.dict['field1']
>    elif name=='field2':
>         return self.dict['field2']
>    else:
>         -- How do I tell him to execute the default ?????
>        
> I want to handle __getattr__ just for some special attributtes names

Just a general question, you seem not to use arbitrary keys for your 
dict, since you have to know them beforehand to be able to index them. 
Why don't you just use normal properties/class attributes?

What I did, when I had to store an arbitrary dict-like structure into 
the zcatalog, was using two attributes of type tuple. Then I wrote 
getter/setter methods which masked the real structure appropriately.

There may be better ways to do things like that.

HTH,
oliver