Understanding super() and mro /u/Electrical_Jicama144 Python Education

I was looking at multiple inheritance and came across a case for this situation there is a base class Z which is inherited by base class B and A and C inherits B and A. C has B first in mro.

If I define same method (overloading) in all and use super().method() from inside C’s method then B’s method is called only but, if I define super().method() in B as well then both B and A are called and if I define super(). method() in both B and A then C,B,A,Z are called in this order. Why is A called when I only define in B and in both B and A even though A is not called in code explicitly? Below is the code and it’s output I got

class Z:

def age(self): print("Age is 2") pass 

class B(Z):

def age(self): print("Age is 23") super().age() 

class A(Z):

def age(self): print("Age is 21") 

class C(B,A):

def age(self): super().age() # This allows the first class function that is inherited by C to be called only here B 

c = C()

c.age()

print(C.mro())

Output

Age is 23

Age is 21

[<class ‘__main__.C’>, <class ‘__main__.B’>, <class ‘__main__.A’>, <class ‘__main__.Z’>, <class ‘object’>]

submitted by /u/Electrical_Jicama144
[link] [comments]

​r/learnpython I was looking at multiple inheritance and came across a case for this situation there is a base class Z which is inherited by base class B and A and C inherits B and A. C has B first in mro. If I define same method (overloading) in all and use super().method() from inside C’s method then B’s method is called only but, if I define super().method() in B as well then both B and A are called and if I define super(). method() in both B and A then C,B,A,Z are called in this order. Why is A called when I only define in B and in both B and A even though A is not called in code explicitly? Below is the code and it’s output I got class Z: def age(self): print(“Age is 2”) pass class B(Z): def age(self): print(“Age is 23”) super().age() class A(Z): def age(self): print(“Age is 21”) class C(B,A): def age(self): super().age() # This allows the first class function that is inherited by C to be called only here B c = C() c.age() print(C.mro()) Output Age is 23 Age is 21 [<class ‘__main__.C’>, <class ‘__main__.B’>, <class ‘__main__.A’>, <class ‘__main__.Z’>, <class ‘object’>] submitted by /u/Electrical_Jicama144 [link] [comments] 

I was looking at multiple inheritance and came across a case for this situation there is a base class Z which is inherited by base class B and A and C inherits B and A. C has B first in mro.

If I define same method (overloading) in all and use super().method() from inside C’s method then B’s method is called only but, if I define super().method() in B as well then both B and A are called and if I define super(). method() in both B and A then C,B,A,Z are called in this order. Why is A called when I only define in B and in both B and A even though A is not called in code explicitly? Below is the code and it’s output I got

class Z:

def age(self): print("Age is 2") pass 

class B(Z):

def age(self): print("Age is 23") super().age() 

class A(Z):

def age(self): print("Age is 21") 

class C(B,A):

def age(self): super().age() # This allows the first class function that is inherited by C to be called only here B 

c = C()

c.age()

print(C.mro())

Output

Age is 23

Age is 21

[<class ‘__main__.C’>, <class ‘__main__.B’>, <class ‘__main__.A’>, <class ‘__main__.Z’>, <class ‘object’>]

submitted by /u/Electrical_Jicama144
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *