Python Call Parent Method Multiple Inheritance
You can use __bases__
like this
class D(A, B, C):
def foo(self):
print("foo from D")
for cls in D.__bases__:
cls().foo("D")
With this change, the output will be
foo from D
foo from A, call from D
foo from B, call from D
foo from C, call from D
Add super()
call's in other classes as well except C
. Since D's MRO is
>>> D.__mro__
(<class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <type 'object'>)
You don't need super call in C
.
Code:
class A(object):
def foo(self, call_from):
print "foo from A, call from %s" % call_from
super(A,self).foo('A')
class B(object):
def foo(self, call_from):
print "foo from B, call from %s" % call_from
super(B, self).foo('B')
class C(object):
def foo(self, call_from):
print "foo from C, call from %s" % call_from
class D(A, B, C):
def foo(self):
print "foo from D"
super(D, self).foo("D")
d = D()
d.foo()
Output:
foo from D
foo from A, call from D
foo from B, call from A
foo from C, call from B