[Tutor] Calling method in parent class
kent37 at tds.net
Tue May 12 13:27:52 CEST 2009
On Tue, May 12, 2009 at 6:32 AM, Jeremiah Dodds
<jeremiah.dodds at gmail.com> wrote:
> If your superclass has a method with the same name (other than __init__
> here), that contains some logic that a subclass that overrides the method
> needs, it's written wrong in python. In this case, use different method
> names, or factor out the parent class methods functionality into (probably)
> a decorator. Code reuse should be strived for, but that's not the only
> purpose of inheritance. If you need to override a method in a subclass, and
> still need to call the parents method in that subclass, you're almost
> definately using inheritance wrong, with the special exception of __init__.
I don't agree with this at all. It's not at all unusual for a derived
class to override a base class method in order to add additional
functionality to it, then to call the base class method to complete
the implementation. __init__() is the most common example but not the
only one. I don't consider this a design flaw at all. Another way to
do this is for the base class method to call a hook method that
subclasses can define but IMO that is often more trouble than it is
I don't see how you would use a decorator to do this.
> In the case of __init__, you probably want to use Parent.__init__, and not
> super, if only because of all the weird edge cases with super.
super() is intended to solve problems that arise when using multiple
inheritance. It has to be used carefully and consistently. Personally
I use the Parent.method(self) style.
More information about the Tutor