Mixin classes and single/multiple inheritance

thebjorn BjornSteinarFjeldPettersen at gmail.com
Thu Sep 20 02:18:01 EDT 2007


On Sep 20, 6:52 am, Ben Finney <bignose+hates-s... at benfinney.id.au>
wrote:
> Michele Simionato <michele.simion... at gmail.com> writes:
> > Since the language we have does have multiple inheritance, let's
> > use it to implement mixins.
> > ...
> > So, multiple inheritance is giving us very little for the point of
> > view of mixins; OTOH, multiple inheritance is giving us a lot of
> > headaches for what concerns cooperative methods.
>
> I may be obtuse, but the above statements seem to be professing
> contradictory viewpoints.

How so? A language doesn't need MI to have mixins (e.g. Ruby does
this). This doesn't seem to contradict that MI is, in general, a
horrible solution to most problems -- one example being the general
difficulty in groking cooperative (super) methods. A single-
inheritance language, otoh, is also a chore to work with (for a
specific subset of designs) -- unless there is _some_ way to do
mixins.  But for the general case I agree with Micele that delegation
is the better solution (Explicit is better than implicit).

> Do you think multiple inheritance should or should not be used in
> Python to do what mixins do?

Michele already answered this, Python has MI so it would just be
confusing to implement mixins any other way.

> > My point was that even if Python had been implemented without
> > multiple inheritance, it would have been simple to implement mixins,
> > or by copying the methods, or by dispatching with __getattr__.
>
> Can you give a code example of how you think mixins should be
> implemented in Python, assuming the absence of multiple inheritance?

Googling for "Ruby mixins tutorial" gave me the following link:
http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/

If I tell you that self.something is written @something in Ruby, and
inheritance is written class Base < Parent (as opposed to class
Base(Parent):) everything else should be understandable..?

How to implement include and extend in Python is left as an excercise
for the reader.

-- bjorn





More information about the Python-list mailing list