0

Bear with me as I am getting started with Python.
Say I have a multiple inheritance scenario:

class Animal:
  def __init__(self, Animal):
    print(Animal, 'is an animal.');

class Mammal(Animal):
  def __init__(self, mammalName):
    print(mammalName, 'is a warm-blooded animal.')
    super().__init__(mammalName)
    
class NonWingedMammal(Mammal):
  def __init__(self, NonWingedMammal):
    print(NonWingedMammal, "can't fly.")
    super().__init__(NonWingedMammal)

class NonMarineMammal(Mammal):
  def __init__(self, NonMarineMammal):
    print(NonMarineMammal, "can't swim.")
    super().__init__(NonMarineMammal)

class Dog(NonMarineMammal, NonWingedMammal):
  def __init__(self):
    print('Dog has 4 legs.');
    super().__init__('Dog')
    
d = Dog()

Upon printing, I get this:

Dog has 4 legs.
Dog can't swim.
Dog can't fly.
Dog is a warm-blooded animal.
Dog is an animal.

When init() is getting called from Dog class, the init() of NonMarineMammal is called which makes sense since MRO starts with left-most base class for a child class in case of multiple inheritance. Again, the init() of NonMarineMammal calls its super()'s init() which is Mammal.
So my confusion is why I got

Dog can't fly.

instead of

Dog is a warm-blooded animal.

Why the above order isn't followed? Is it something to do with init() function or the same behaviour stands for other methods as well?

Sandeep Kumar
  • 53
  • 1
  • 4
  • You sometimes have the class name in your init method instead of "animalname" or something. I don't know if that's a problem, but it is confusing to read at least. – Wups Sep 21 '20 at 20:00
  • Does this answer your question? [How does Python's super() work with multiple inheritance?](https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance) – Wups Sep 21 '20 at 20:17

0 Answers0