0

I have the following python code

class A:
    def __init__(self):
        print("A", end=" ")
        super().__init__()

class B:
    def __init__(self):
        print("B", end=" ")
        super().__init__()

class C:
    def __init__(self):
        print("C", end=" ")
        super().__init__()

class D(A, B, C):
    def __init__(self):
        print("D", end=" ")    
        #B.__init__(self)
        A.__init__(self)
        #super().__init__()


if __name__ == "__main__":
    D()

The output of this code peer to my understanding had to be

D A

But the output is

D A B C

And if I remove the super().__init__() part in class A, I get the following result

D A

My question is, why does the initialization of B and C occur when super().__init__() is placed in class A? As I know, super method is for accessing the parent class, but in this case it seems to be responsible for something else.

Karen Baghdasaryan
  • 1,321
  • 4
  • 19
  • 1
    Welcome back to Stack Overflow. "As I know, super method is for accessing the parent class" This is not correct. Please read https://meta.stackoverflow.com/questions/261592. In this case, getting the necessary information is as easy as putting something like `python super multiple inheritance` [into a search engine](https://duckduckgo.com/?q=python+super+multiple+inheritance). – Karl Knechtel Feb 24 '22 at 23:50
  • @KarlKnechtel you are right! Perhaps I should have put more effort in using search engines :) Anyway thank you very much! – Karen Baghdasaryan Feb 24 '22 at 23:55
  • "As I know, super method is for accessing the parent class" only the case of simple inheritance. What it *really* does is get the *next class in the method resolution order*, which in the case of simple inheritance is just "the parent", but that isn't true in the case of multiple inheritance. – juanpa.arrivillaga Feb 25 '22 at 00:21

0 Answers0