[Tutor] Calling method in parent class

Jeremiah Dodds jeremiah.dodds at gmail.com
Tue May 12 14:44:56 CEST 2009


On Tue, May 12, 2009 at 12:27 PM, Kent Johnson <kent37 at tds.net> wrote:

> 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
> worth.
>
> I don't see how you would use a decorator to do this.
>

Can you give an example? I've never seen this done, outside of __init__, in
a way where it wouldn't be clearer if the base class's methodname was just
named something else, and then have the Parent class's eventually overriden
method just call the renamed method. Put the common functionality in a
method that's not overriden.

Generally, I see subclasses adding methods to base classes and entirely
overriding methods, and nothing more.

Depending on what you need to do, you can pull out what would normally be in
the base classes method into a decorator that calls the subclassess method
and then does what it needs to do. I don't see this too often.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090512/ccc60be8/attachment.htm>


More information about the Tutor mailing list