advice on programming style: is multiple inheritance bad?

John Roth newsgroups at jhrothjr.com
Sun Feb 1 14:56:33 EST 2004


"Uwe Mayer" <merkosh at hadiko.de> wrote in message
news:bvjhkv$ss4$1 at news.rz.uni-karlsruhe.de...
> Hi,
>
> I got a class A2 that needs to inherit from class A1.
> A2, as well as some other classes implement a same functionality and in
> order to prevent code duplication I'd like to factorize that (potentially)
> duplicate code into a class SuperA from which then A2 and all other
classes
> could inherrit.
> However, this would cause A2 to have two super classes A1 and SuperA -
which
> is possible of course in Python.
>
> My question is: is that bad programming style?
> From C++ and Java we "learn" that you shouldn't do that.
>
> Class A is automatically generated from other tools, so I can't solve the
> problem by just letting A1 inherit from SuperA.
>
> Your opinions?

I think it's a case of "it depends." There are places where multiple
inheritance solves a real problem, and places where using it is just
going to confuse the issue.

Languages that try to ban multiple inheritance quickly invent things
like interfaces and mixins, so there are obviously some uses for
the facility.

Some rules of thumb I like:

1. Is it a special case, like Java's interfaces or Ruby's mixins?
If so, you're probably on firm ground.

2. Have you thoroughly considered using composition instead
of inheritance? Inheritance is vastly oversold.

3. Does the same attribute show up on two different
legs you inherit from? That's a red flag: you probably need
to rethink the design.

John Roth


>
> Ciao
> Uwe
> -- 
>





More information about the Python-list mailing list