A class 'Employee' has a method 'work()'. A subclass 'Manager' overrides 'work()' but also wants to call the parent's 'work()'. Which syntax correctly achieves this?
Trap 1: self.work()
self.work() performs a normal attribute lookup on the self instance, which means the method resolution starts from the class of self — in this scenario, Manager. Since Manager.work() is the method currently executing, calling self.work() inside that method will simply dispatch to the same overridden method again, creating an infinite recursive loop. This is not a delegation to the parent at all; it is a dynamic dispatch that honors the subclass override, so it can never reach Employee.work().
- A
self.work()
Why it fails: self.work() performs a normal attribute lookup on the self instance, which means the method resolution starts from the class of self — in this scenario, Manager. Since Manager.work() is the method currently executing, calling self.work() inside that method will simply dispatch to the same overridden method again, creating an infinite recursive loop. This is not a delegation to the parent at all; it is a dynamic dispatch that honors the subclass override, so it can never reach Employee.work().
- B
super().work()
super().work() is the Python 3 idiom that returns a proxy object bound to the enclosing class, and when its work() method is invoked, the actual lookup begins in the __mro__ of the runtime instance, immediately after the current class. This respects cooperative multiple inheritance and the true method resolution order, ensuring that the next logical class in the MRO (usually the parent) is called. It is the recommended, concise way to delegate to the parent's implementation without hardcoding a class name.
- C
super(Manager, self).work()
super(Manager, self).work() is a valid syntax from Python 2, but in Python 3, super().work() is preferred. While it works, it is not the simplest or recommended approach, so it is considered incorrect for this question.
- D
Employee.work(self)
Employee.work(self) directly calls the parent class method without using super(). This works in simple single inheritance but fails with multiple inheritance and does not follow cooperative method resolution, so it is not the correct approach.