Question

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

class B(A):
    def foo(self):
        print 'B'

class C(B):
    pass

c = C()
c.foo()
>>> B

I want to call the foo() method of class A. So how can I call this foo() method so that it will print 'A'

Was it helpful?

Solution

To directly answer your question, you could define C like this:

class C(B):
    def foo(self):
        A.foo(self)

However, a better question might be: why are you trying to do this? A class needing to access functions from the, erm, "grandparent" that were also defined in the parent class is a pretty good sign that your inheritance model is not ideal. For example: does C need anything from B; could it inherit directly from A?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top