Inheritance problem?

Mike Meyer mwm at mired.org
Fri Jan 6 12:48:08 EST 2006


Xavier Morel <xavier.morel at masklinn.net> writes:
> Pierre Barbier de Reuille wrote:
>> Well, I would even add : don't use super !
>> Just call the superclass method :
>> MyClass.__init__(self)
>> Simon Percivall a écrit :
>>> Don't use self.__class__, use the name of the class.
> Bad idea if you're using new-style classes with a complex inheritance
> hierarchy and multiple inheritance.

To quote the original code:

class MyClass(MyBaseClass)
   def __init__(self)
      super(self.__class__, self).__init__()
      self.type = MyClassType
      return self

class MySpecialClass(MyClass)
   def __init__(self)
      super(self.__class__, self).__init__()
      self.type = MySpecialClassType
      return self

The only place it uses self.__class__ is in the calls to super. Super
finds the superclass of it's first argument. If that argument is
self.__class__, then super will always return the superclass of the
class of self, *not* the superclass of the class who's code is being
run. That's why the code resuls in an infinite recursion.

And a note to the OP: __init__'s return value is ignored. You should
delete the "return self" from your methods.

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list