Problems with inheritance in python base products
Hello, I want to design 2+ products which all share certain base functionality. So I moved some code into Shared. Now, when I try to create instances of the products I get the following error: Error Type: TypeError Error Value: unbound method must be called with class instance 1st argument I use the following code: class B(A): def __init__(self, some, more, parameters): A.__init__(self, some, more) In my python knowlegde the above should be correct... Any hints... Sascha PS: I know ZClasses, but I want to use pure python for comfort reasons... -- .-> Sascha Matzke - sascha@bespin.de - http://www.bespin.de -. | Das Kapital ist die Flamme, der Faschismus das Benzin | | Anarchist Academy | `-- On this earth for 24 years, 3 days <----------------'
At 04:06 17-10-99 , Sascha Matzke wrote:
Hello,
I want to design 2+ products which all share certain base functionality. So I moved some code into Shared.
Now, when I try to create instances of the products I get the following error:
Error Type: TypeError Error Value: unbound method must be called with class instance 1st argument
I use the following code:
class B(A):
def __init__(self, some, more, parameters): A.__init__(self, some, more)
In my python knowlegde the above should be correct...
This is either caused by ExtensionClass or acquisition, I never bothered to get to the heart of it. It basically confuses the Python interpreter into not seeing the proper class hierarchy, and throwing above tantrum. The solution is simple: call .im_func() to use the unbound method: def __init__(self, some, more, parameters): A.__init__.im_func(self, some, more) -- Martijn Pieters, Web Developer | Antraciet http://www.antraciet.nl | T: +31 35 7502100 F: +31 35 7502111 | mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ---------------------------------------------
The problem stems from the use of ExtensionClass, this snippet from http://www.digicool.com/releases/ExtensionClass explains : ---------------------------------------------------------------------------- -------------------- A problem occurs when trying to override methods inherited from Python base classes. Consider the following example: from ExtensionClass import Base class Spam: def __init__(self, name): self.name=name class ECSpam(ExtensionClass.Base, Spam): def __init__(self, name, favorite_color): Spam.__init__(self,name) self.favorite_color=favorite_color This implementation will fail when an ECSpam object is instantiated. The problem is that ECSpam.__init__ calls Spam.__init__, and Spam.__init__ can only be called with a Python instance (an object of type "instance") as the first argument. The first argument passed to Spam.__init__ will be an ECSpam instance (an object of type ECSPam). ---------------------------------------------------------------------------- -------------------- In order to access the base you need to use : B.inheritedAttribute('__init__')(self,some, more) Unfortunately, this only works in single inheritance hierarchies. Robert Leftwich
-----Original Message----- From: Sascha Matzke [mailto:sascha@bespin.escape.de]On Behalf Of Sascha Matzke Sent: Sunday, 17 October 1999 12:07 To: zope@zope.org Subject: [Zope] Problems with inheritance in python base products
Hello,
I want to design 2+ products which all share certain base functionality. So I moved some code into Shared.
Now, when I try to create instances of the products I get the following error:
Error Type: TypeError Error Value: unbound method must be called with class instance 1st argument
I use the following code:
class B(A):
def __init__(self, some, more, parameters): A.__init__(self, some, more)
In my python knowlegde the above should be correct...
Any hints...
Sascha PS: I know ZClasses, but I want to use pure python for comfort reasons... -- .-> Sascha Matzke - sascha@bespin.de - http://www.bespin.de -. | Das Kapital ist die Flamme, der Faschismus das Benzin | | Anarchist Academy | `-- On this earth for 24 years, 3 days <----------------'
_______________________________________________ Zope maillist - Zope@zope.org http://www.zope.org/mailman/listinfo/zope
(Related lists - please, no cross posts or HTML encoding!
To receive general Zope announcements, see: http://www.zope.org/mailman/listinfo/zope-announce
For developer-specific issues, zope-dev@zope.org - http://www.zope.org/mailman/listinfo/zope-dev )
Hello, On Sun, Oct 17, 1999 at 06:44:10PM +1000, Robert Leftwich wrote:
In order to access the base you need to use :
B.inheritedAttribute('__init__')(self,some, more)
Unfortunately, this only works in single inheritance hierarchies.
Ok, I tried this... I have following __init__ in the superclass: class A: def __init__(self, REQUEST): If I use class B(ExtensionClass.Base, A): def __init__(self, some, other, REQUEST): B.inheritedAttribute('__init__')(self, REQUEST) I get following error.. Error Type: TypeError Error Value: too many arguments; expected 1, got 2 Which - IMO - shouldn't happen. Sascha -- .-> Sascha Matzke - sascha@bespin.de - http://www.bespin.de -. | Macht kaputt was euch kaputt macht... | | Ton Steine Scherben | `-- On this earth for 24 years, 3 days <----------------'
participants (4)
-
Martijn Pieters -
Robert Leftwich -
Sascha Matzke -
Sascha Matzke