[Python] How python MRO works in hierarchical and multiple inheritance

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    So far it's making me confuse, how below code to understand for the given result. Can someone explain below two parts of output how actually flow is going on. First part giving output as 'C' and second part is 'A'

    class A:
    def fun1(self):
    print("A")

    class B(A):
    def fun1(self):
    print('B')

    class C(A):
    def fun1(self):
    print("C")

    class D(B, C):
    def fun1(self):
    super(B, self).fun1()

    obj = D()
    obj.fun1() **# How output will be 'C' in this case ?**


    output : C


    class A:
    def fun1(self):
    print("A")

    class B(A):
    def fun1(self):
    print("B")

    class C(A):
    def fun1(self):
    print("C")

    class D(B, C):
    def fun1(self):
    super(C, self).fun1()

    obj = D()
    obj.fun1() **# How output will be 'A' in this case ?**


    output : A

    Continue reading...

Compartilhe esta Página