How does a super method actually works in python?
In the given code:
class A(object):
def test(self):
return 'A'
class B(A):
def test(self):
return 'B->'+super(B, self).test()
class C(A):
def test(self):
return 'C'
class D(B,C):
pass
print B().test() # B->A
print D().test() # B->C ????
# MRO of classes are as
print 'mro of A', A.__mro__ # [A,object]
print 'mro of B', B.__mro__ # [B,A,object]
print 'mro of C', C.__mro__ # [C,A,object]
print 'mro of D', D.__mro__ # [D,B,C,A,object]
when test is called on D, it output B->C instead of B->A (which I was expecting).
How is super inside B referring to an instance of C?
testinDand call whichever base you want explicitly or better yet rewrite the method entirely. Python is duck-typed, you are trying to use classical typed inhretitance. ref: en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem