Run a specific parent’s method from a child class

This is an example of how to run a specific parent’s method from a child class in Python.

class A(object):
    def foo(self):
        print('A.foo()')
        self.run()

    def run(self):
        print("A run")


class B(object):
    def foo(self):
        print('B.foo()')
        self.run()

    def run(self):
        print("B run")


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

c = C()
c.foo()

 

Results:

A.foo()
A run
B.foo()
A run

Leave a comment

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