Help subclassing a non Zope class as base class of a product
Hi, I got a strange behaviour when subclassing a non Zope class (called MyBase) that has - and needs - an __init__(...) by another class that provides Zope features (called MyZSub). from OFS.SimpleItem import SimpleItem from mybase import MyBase ... def manage_addMyZSub(self, id, title='', REQUEST=None): o = MyZSub(id, title) self._setObject(id, o) ... class MyZSub(SimpleItem, MyBase): ... def __init__(self, id, title=''): self.id = id self.title = title # isinstance(self, MyZSub) -> 1 :) # isinstance(self, MyBase) -> 0 :( # (very strange) # THIS RAISES THE EXCEPTION BELOW MyBase.__init__(self) ... Creating a new MyZSub object raises this !!! Error Type: TypeError Error Value: unbound method __init__() must be called with instance as first argument. I can't get what happens. Is "self" something else than the newly created object when in such a class !!!??? When removing this line, it works (except of course there's no construction of the base class 8(( This (MyBase.__init__(self)) works perfectly when subclassing MyBase by any non Zope test class - including with multiple inheritance !!! I can't understand why "self" is not instance of MyBase !!!
class a: pass class b: pass class c(a, b): pass x=c() isinstance(x, a) 1 isinstance(x, b) 1
I made a quick hack tour in the Zope products installed in my serer and I didn't notice any product trying to call the constructor of its base class from its own constructor. Has someone a hint. Please, I'm falling into a nervous breakdown... Thanks in advance. --Gilles
Gilles Lenfant wrote:
I made a quick hack tour in the Zope products installed in my serer and I didn't notice any product trying to call the constructor of its base class from its own constructor.
Has someone a hint. Please, I'm falling into a nervous breakdown...
Well most of us has been there at one time or another ... :-) What you are banging against is the exetensionclass that makes funny stuff with Python. As far as I remember you should: class myObject(ObjectManager) def __init__(self, id): "Inits the object" myObject.inheritedAttribute('__init__')(self, id) Anyhoo you can google on "inheritedAttribute" and stuff should show up. regards Max M
Hi, Thanks Max, but it seems to work only if the base class is a "root" class (does not inherit of an other one). In my case, I have... <SNIP> from UserList import UserList class Myclass(UserList): def __init__(self, data=[]): ... # Exception raises from here now!!! UserList.__init__(self, data) ... class ZMyClass(SimpleItem, MyClass): ... def __init__(self, id, title=''): ... ZMyClass.inheritedAttribute('__init__')(self) ... </SNIP> I could rework my code to have... class ZMyClass(SimpleItem, MyClass, UserList): ... But in that case (multiple __init__ due to multiple inheritance) WHERE does the inheritedAttribute finds the method. Is there some doc for Python product programmers where I could find other APIs guides ? There's - approximately - nothing for this in the Zope book. I did not find anything and hack other's code doing things other do whithout knowing why. In example, I did not find the difference between initializeClass and initializeBaseClass. Thanks again. --Gilles ----- Original Message ----- From: "Max M" <maxm@mxm.dk> To: "Gilles Lenfant" <gilles@objectis.net> Cc: <zope@zope.org> Sent: Tuesday, May 21, 2002 4:05 PM Subject: Re: [Zope] Help subclassing a non Zope class as base class of a product : : As far as I remember you should: : : class myObject(ObjectManager) : : def __init__(self, id): : "Inits the object" : myObject.inheritedAttribute('__init__')(self, id) : : Anyhoo you can google on "inheritedAttribute" and stuff should show up. : : regards Max M : : : : _______________________________________________ : Zope maillist - Zope@zope.org : http://lists.zope.org/mailman/listinfo/zope : ** No cross posts or HTML encoding! ** : (Related lists - : http://lists.zope.org/mailman/listinfo/zope-announce : http://lists.zope.org/mailman/listinfo/zope-dev ) :
participants (2)
-
Gilles Lenfant -
Max M