How do you call a specific ancestor's method explicitly, bypassing the normal MRO-based super() resolution?
Short answer
You can call a specific base class's method directly by name, e.g., `Base.method(self, ...)`, rather than using super(), which bypasses cooperative MRO chaining entirely and calls exactly that class's implementation.
Advertisement
AModel answer
You can call a specific base class's method directly by name, e.g., `Base.method(self, ...)`, rather than using super(), which bypasses cooperative MRO chaining entirely and calls exactly that class's implementation. This is sometimes necessary for edge cases but breaks the cooperative multiple inheritance pattern and can cause a method to be skipped or called multiple times if used carelessly alongside other super() calls in the hierarchy.
class A:
def greet(self): print('A')
class B(A):
def greet(self): print('B')
class C(B):
def greet(self):
A.greet(self) # explicitly calls A's version, skipping B entirely
print('C')
C().greet() # A, C -- B.greet was intentionally bypassedReal-world example
A subclass needs to specifically call a grandparent class's original implementation, skipping an intermediate parent's override, so it calls Grandparent.method(self) directly instead of using super().
Likely follow-ups
- Why is this considered less safe than using super() consistently?
- When is bypassing the MRO chain actually the correct choice?
Related topics