Extending classes __init__behavior for newbies

Ben Finney ben+python at benfinney.id.au
Sun Feb 13 18:19:44 EST 2011


James Mills <prologic at shortcircuit.net.au> writes:

> On Mon, Feb 14, 2011 at 8:21 AM, MRAB <python at mrabarnett.plus.com> wrote:
> > I would've done it this way:
> >
> > class FasterShip(Ship):
> >    def __init__(self, speed=0, **kwargs):
> >        Ship.__init__(self, **kwargs)
> >        self.speed = speed
>
> What's the difference between calling the base
> class's constructor directly and using the super type ?

The difference arises in multiple inheritance. Your class can never know
whether it is used as part of a multiple-inheritance hierarchy, in which
case calling the base class directly would be the wrong thing to do
since that might not match the MRO.

However, using ‘__super__’ properly is very problematic. A case can be
made that it's an inherent problem of multiple inheritance, which should
therefore be avoided. Search the archives of this forum to learn more.

-- 
 \       “If you don't know what your program is supposed to do, you'd |
  `\                 better not start writing it.” —Edsger W. Dijkstra |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list