[Zope] Ran out of Zen: Multiple inheritance and __init__ problems

Dieter Maurer dieter@handshake.de
Tue, 15 Jan 2002 20:54:51 +0100


Lennart Regebro writes:
 > Sometimes when I inherit from multiple classes, I can do thusly:
 > 
 > class C( A, B):
 > 
 >     def __init__(self, foo):
 >         A.__init__(self, foo)
 >         B.__init__(self)
 > 
 > But sometimes this doesn't work, but python complains and sais that unbound
 > methods must be called with class instance 1st argument, although that is
 > exactly what I do.
 > 
 > Sometimes I can do this to solve that problem:
 >     def __init__(self, foo):
 >         A.__init__(self, foo)
 >         C.inheritedAttribute(__init__)(self)
 > 
 > And sometimes not even that works. And yes, I do restart the server without
 > anything changing.
 > Could somebody please try to make some kind of explanation of what is going
 > on and why? I don't understand this problem at all. Is there some known
 > caveats with these methods?
This is documented in the "ExtensionClass" documentation.
There is one in any Zope distribution: "lib/Components/ExtensionClass/doc".

"inheritedAttribute" is good enough for single inheritance but
can give you only a single method.

For multiple inheritance, you wrap your plain python classes
into an ExtensionClass.Base (and use there the "inheritedAttribute")
and then use the ExtensionClasses as base classes of your inherited
class.

In your example above:

   class A_wrapped(ExtensionClass.Base,A):
     def __init__(self,foo):
       A_wrapped.inheritedAttribute('__init__')(self,foo)

   class B_wrapped(ExtensionClass.Base,B):
     def __init__(self):
       B_wrapped.inheritedAttribute('__init__')(self)

   class C(A_wrapped,B_wrapped):
     def __init__(self,foo):
       A_wrapped.__init__(self,foo)
       B_wrapped.__init__(self)

Of course, you can name "A_wrapped" "A" and "B_wrapped" "B".
The suffix was just to make clear what happens.


Dieter