Python inheritance – how to call grandparent method?

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)