0

in multiple inheritance how super() works? for example here I have Two init and I want to send args by super():

class LivingThings(object):
    def __init__(self, age ,name):
        self.name=name
        self.age=age

    def Print(self):
        print('age: ', self.age)
        print('name: ', self.name)

class Shape(object):
    def __init__(self, shape):
        self.shape=shape
    def Print(self):
        print(self.shape)

class Dog(LivingThings, Shape):
    def __init__(self, breed, age , name, shape):
        self.breed=breed
        super().__init__(age , name)
        super().__init__(shape)


    def Print(self):
        LivingThings.Print(self)
        Shape.Print(self)
        print('breed', self.breed)

but error :

super().__init__(shape)
TypeError: __init__() missing 1 required positional argument: 'name'

but this code works :

class Dog(LivingThings, Shape):
    def __init__(self, breed, age , name, shape):
        self.breed=breed
        Shape.__init__(self, shape)
        LivingThings.__init__(self,age ,name)

so super() dosent work in multiple inheritance??

user2864740
  • 60,010
  • 15
  • 145
  • 220
Zero Days
  • 851
  • 1
  • 11
  • 22
  • It would if you implemented it correctly - neither superclass uses `super`, so only the first one gets called, and they have different signatures. – jonrsharpe Sep 27 '15 at 08:06
  • As well as adding appropriate `super()` calls to those `__init__` methods you need to change them so they all have the same call signature. The simple way to do that is to use a dict of keyword args, as shown in the `ColoredShape` example in the Raymond Hettinger article linked by Daniel Roseman. – PM 2Ring Sep 27 '15 at 08:09

1 Answers1

4

super works fine in multiple inheritance; that is in fact precisely what it is for. But for some reason you are calling it twice, with different arguments; that is not how it works.

Call it once. That calls the next method in the method resolution order. It is then that method's responibility to call super, to call the next method.

Please read Raymond Hettinger's classic article Super considered super.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895