Interfaces != Multiple Inheritance [was Re: python development practices?]

Luigi Ballabio ballabio at mac.com
Wed Oct 31 05:35:25 EST 2001


At 06:35 PM 10/30/01 -0800, Paul Rubin wrote:
>Richard Jones <richard at bizarsoftware.com.au> writes:
> > Java interfaces != multiple inheritance.
> >
> > They address completely different issues. Mutliple inheritance allows one
> > to include functionality from multiple base classes in one step. An
> > interface indicates that your class implements a specific set of methods.
> > Interfaces don't implement functionality, therefore indicating that your
> > class implements multiple interfaces doesn't add any additional
> > functionality.
>
>The idea is implement an abstract base class instead of an interface.
>Define methods for all the operations you want your interface to
>support, and make each method simply raise NotImplementedError.  Then
>make the implementation for your inheriting class supply real versions
>of all the operations.

Why would you want to do that in Python?

The interface mechanism is needed in Java due to its type checking---if you 
declare a function as f(Interface i), then an object needs to implement 
Interface to be accepted by f as an argument. The same applies to C++ and 
purely abstract base classes. There is no such requirement in Python: you 
just write f(i) assuming that i implements the needed methods, and the 
interpreter will raise an error if i doesn't. The principle is the same 
(well, alike) as C++ templates.

Bye,
         Luigi





More information about the Python-list mailing list