0

class M and F are two standalone classes. Class C inherits both F and M . I don't understand why the super() keyword of class F is calling the init of class M . Can someone explain the working behind this code?

 class M:
       def __init__(self):
         print("M constructor")


class F:
       def __init__(self):
          print("F constructor")
          super().__init__()
          super().__init__()

class C(F,M):
     def __init__(self):
        super().__init__()
        print("C constructor")

C()

The O/P

F constructor

M constructor

M constructor

C constructor

arin
  • 29
  • 4
  • https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance – imperosol Jun 13 '22 at 13:41
  • 1
    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) – imperosol Jun 13 '22 at 13:42
  • 3
    `super()` doesn't really mean "the parent of this class"; it means "the next class in the method resolution order", which can be something entirely different in the case of multiple inheritance. – jasonharper Jun 13 '22 at 13:42
  • *"why the super() keyword of class F is calling the init of class M"* Because it is still a `C` instance. Add `print(super().__init__)` (without `()`) and see. – DeepSpace Jun 13 '22 at 13:48

0 Answers0