How do Java interfaces translate to Python?

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Oct 1 12:57:45 EDT 2001


Jan Samohyl <jan at blackbox.hell> writes:

> On Sun, Sep 30, 2001 at 10:50:15PM +0000, Gordon Tyler wrote:
> > In article <j44rpk2unm.fsf at informatik.hu-berlin.de>, Martin von Loewis <loewis at informatik.hu-berlin.de> wrote:
> > > What exactly is it that you do with interfaces in Java?
> > 
> > I find myself unable to think of anything other than that. Which suggests
> > to me that perhaps interfaces aren't such a useful construct after all. ;)
> 
> I have heard somewhere that in java one often uses interfaces where he would 
> use multiple inheritance. Maybe this is the case.

This case occurs if you need a multi-rooted hierarchy. I.e there are
some object which are phones (i.e. have a getNumber operation), and
some things that are portable (i.e. support a getWeight
operation). Some things are both portable and phones. That sounds like
you'd need multiple inheritance.

In Java, you can do this with interfaces; provide a Phone and a
Portable interface, and have the 'Handy' device implement both of
them. Then you can pass that device both to somebody trying to call
you, and to somebody carrying things for you.

In Python, you can just implement both getNumber and getWeight,
without having to declare any interfcaces, and pass Handys to
algorithms that expect either kind of object.

Regards,
Martin



More information about the Python-list mailing list