48

Consider the following piece of code:

class A:
  def foo(self):
    return "A"

class B(A):
  def foo(self):
    return "B"

class C(B):
  def foo(self):
    tmp = ... # call A's foo and store the result to tmp
    return "C"+tmp

What shall be written instead of ... so that the grandparent method foo in class A is called? I tried super().foo(), but it just calls parent method foo in class B.

I am using Python 3.

karlosss
  • 2,816
  • 7
  • 26
  • 42
  • This is the exact same case with this question asked 3 days ago: https://stackoverflow.com/questions/42937114/python-pattern-for-defaulting-to-a-grandparent-classs-implementation – coder Mar 25 '17 at 13:20
  • 2
    Wanting to do this seems like a clear sign that your inheritance is wrong. I strongly suggest fixing the code so that you don't need to skip over a parent. – Ned Batchelder Mar 25 '17 at 13:21
  • Does this answer your question? [Calling a parent's parent's method, which has been overridden by the parent](https://stackoverflow.com/questions/18117974/calling-a-parents-parents-method-which-has-been-overridden-by-the-parent) – vvv444 May 30 '23 at 11:50
  • Even though this question happens to have good answers, the second has good answers as well. But this was asked 4 years later. So should be closed according to these guidelines: https://meta.stackexchange.com/questions/10841/how-does-duplicate-closing-work-when-is-a-question-a-duplicate-and-how-should – vvv444 May 30 '23 at 11:51

4 Answers4

60

There are two ways to go around this:

Either you can use A.foo(self) method explicitly as the others have suggested - use this when you want to call the method of the A class with disregard as to whether A is B's parent class or not:

class C(B):
  def foo(self):
    tmp = A.foo(self) # call A's foo and store the result to tmp
    return "C"+tmp

Or, if you want to use the .foo() method of B's parent class regardless of whether the parent class is A or not, then use:

class C(B):
  def foo(self):
    tmp = super(B, self).foo() # call B's father's foo and store the result to tmp
    return "C"+tmp
Alexander Rossa
  • 1,900
  • 1
  • 22
  • 37
2

You can simply be explicit about the class. super() allows you to be implicit about the parent, automatically resolving Method Resolution Order, but there's nothing else that's special about it.

class C(B):
    def foo(self):
       tmp = A.foo(self)
       return "C"+tmp
Julien
  • 5,243
  • 4
  • 34
  • 35
2

Calling a parent's parent's method, which has been overridden by the parent There is an explanation in this discuss already on how to go back in the tree.

Community
  • 1
  • 1
Zed Evans
  • 126
  • 1
  • 12
1

You can explicity call the method from the grandparent class:

class C(B):
    def foo(self):
       tmp = A.foo()
       return "C" + tmp
Robert Valencia
  • 1,752
  • 4
  • 20
  • 36