[Zope] Attribute lookup

Paul Winkler pw_lists at slinkp.com
Tue Nov 4 00:57:58 EST 2003


On Fri, Oct 31, 2003 at 03:24:46PM -0300, Gabriel Genellina wrote:
> Hello
> 
> I have a class A, and another class B which is a wrapper for it (a Product, 
> inherited from SimpleItem.SimpleItem). I want B to delegate most attributes 
> to A:
> 
> class A:
>         def __init__(self, title, ...):
>                 self.title = title
>                 ....
> 
> class B(SimpleItem.SimpleItem):
>         def __init__(self, id, title):
>                 self.id = id
>                 self.innerA = A(title)
> 
>         def __getattr__(self, name):
>                 return getattr(self.__dict__['innerA'], name)
> 
> This works fine for most attributes *but* title. SimpleItem.Item has a 
> class attribute 'title' and, altough B instances have no 'title' attribute 
> at all, they inherit an empty class attribute 'title' from Item, and never 
> goes thru __getattr__.
> How could I arrange things so 'title' comes from inner A instance, and 
> *not* from Item base class?

you could have B.title be a ComputedAttribute
that returns innerA.title.

Be sure you test your class thoroughly. I found overloading __getattr__ 
in zope so painful that i gave up trying.  It can be done 
(I believe PortableHole does it). The difficulties stem from the
fact that __getattr__ is already heavily used by the security 
and acquisition machinery. 

-- 

Paul Winkler
http://www.slinkp.com
Look! Up in the sky! It's ULTRA ACCOUNTANT!
(random hero from isometric.spaceninja.com)



More information about the Zope mailing list