[Python-Dev] python and super

Steven D'Aprano steve at pearwood.info
Thu Apr 14 15:43:52 CEST 2011


Ricardo Kirkner wrote:
> Hi all,
> 
> I recently stumbled upon an issue with a class in the mro chain not
> calling super, therefore breaking the chain (ie, further base classes
> along the chain didn't get called).
> I understand it is currently a requirement that all classes that are
> part of the mro chain behave and always call super. My question is,
> shouldn't/wouldn't it be better,
> if python took ownership of that part, and ensured all classes get
> called, even if some class misbehaved?

Consider the difference between extending the method and replacing it. 
(I've always known that as "overloading" and "overriding", but the 
terminology varies.) If Python automagically always called super(), how 
would you replace a method?

For that matter, at which point would you automagically call super()? At 
the start of the overloaded method, before the subclass code runs? At 
the end, after the subclass code? Somewhere in the middle?

class Spam(Ham):
     def method(self):
         # Overload method.
         super().method()  # at the start of the method?
         do_stuff()
         super().method()  # in the middle of the method?
         do_more_stuff()
         super().method()  # or at the end of the overloaded method?


What arguments should be passed? What do you do with the result?

If you can think of a way for Python to automagically tell when to call 
super(), what arguments to pass to it, and what to do with the result, 
your crystal ball is better than mine.


-- 
Steven



More information about the Python-Dev mailing list