[Zope] Attribute lookup
J. Cameron Cooper
jccooper at jcameroncooper.com
Mon Nov 3 22:56:30 EST 2003
> 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?
A curious arrangement. Take a look at this little experiment on a Python
console::
>>> class A:
... title = 'dog'
...
>>> class I:
... title = 'cat'
...
>>> class B(I):
... innerA = A()
... title = A.title
...
>>> B.title
'dog'
Looks like that works as you want.
--jcc
--
"My point and period will be throughly wrought,
Or well or ill, as this day's battle's fought."
More information about the Zope
mailing list