Ran out of Zen: Multiple inheritance and __init__ problems
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?
On Mon, 2002-01-14 at 18:18, Lennart Regebro wrote:
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.
I can't offer any help, but I can confirm I get this too. I only ever get it in combination with the Refresh product, e.g. - Make a change to a product - Refresh it - Try to view an instance - Get unbound method 1st argument blah message - Restart Zope - Everything works seb
From: "seb bacon" <seb@jamkit.com>
I can't offer any help, but I can confirm I get this too. I only ever get it in combination with the Refresh product, e.g.
- Make a change to a product - Refresh it - Try to view an instance - Get unbound method 1st argument blah message - Restart Zope - Everything works
I do sometimes not get it to work even after a restart... :-/ Usually inheritedAttribute works then. Oh, I forgot to say, I'm using 2.4.3 still. (Which means Python 1.5.2). Maybe thats significant.
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
From: "Dieter Maurer" <dieter@handshake.de>
There is one in any Zope distribution: "lib/Components/ExtensionClass/doc".
I love Google. I just cut out from above, paste it into my Google toolbar searchbox, and press 'I'm feeling lucky' and whaddayouknow, I end up straight in the correct place in cvs.zope.org. Fantastic. :-) Anyway, making sure both classes subclassed ExtensionClass.Base made the first alternative work as expected. This is the drawback of learning Python the same time as Zope. A lot of magick is done in most Zope classes that you don't know about, and hence, when you aren't subclassing Zopeclasses, probles arise. :-) Thanks!
participants (3)
-
Dieter Maurer -
Lennart Regebro -
seb bacon