Interview Questions

How do I call a method defined in a base class from a derived class that overrides it?

Python Questions and Answers


(Continued from previous question...)

How do I call a method defined in a base class from a derived class that overrides it?

If you're using new-style classes, use the built-in super() function:

class Derived(Base):
def meth (self):
super(Derived, self).meth()

If you're using classic classes: For a class definition such as class Derived(Base): ... you can call method meth() defined in Base (or one of Base's base classes) as Base.meth(self, arguments...). Here, Base.meth is an unbound method, so you need to provide the self argument.

(Continued on next question...)

Other Interview Questions