Search code examples
pythonmultiple-inheritance

python multiple inheritance from different paths with same method name


With the following code sample, can super be used, or C has to call A.foo and B.foo explicitly?

class A(object):
    def foo(self):
        print("A.foo()")


class B(object):
    def foo(self):
        print("B.foo()")


class C(A, B):
    def foo(self):
        print("C.foo()")
        A.foo(self)
        B.foo(self)

Solution

  • super() will only ever resolve a single class type for a given method, so if you're inheriting from multiple classes and want to call the method in both of them, you'll need to do it explicitly. i.e.A.foo(self)