newbie OOP question (what's the analogue to java interfaces in Python?)

Benjamin Kaplan benjamin.kaplan at case.edu
Thu Jan 15 23:57:34 CET 2009


On Thu, Jan 15, 2009 at 4:28 PM, Larry Caruso <lcaruso at tds.net> wrote:

>  I'm an old time C programmer, but a newbie to OOP and Python. One OOP
> book I'm reading, "Holub on Patterns," discusses Java Interfaces in some
> detail (see http://en.wikipedia.org/wiki/Interface_(Java) for example) but
> with limited time, I'm hoping to avoid learning Java just to understand some
> key points of this book. Can anyone tell me what the analogue, if any, would
> be in Python? TIA.
>
>
>

There isn't one (well, you could use Abstract Base Classes if you're using
Python 3, but you don't need to). Interfaces are needed in Java for 2
reasons.
1. There are times when designing something that you don't really care what
the class actually is, just what it does (i.e. iterators). With Java's
static typing, you either have to overload the method for each object that
needs to work with this method or use inheritance. Python uses duck typing-
if it looks like a duck and quacks like a duck, it must be a duck. No static
typing= no need to inherit. Inheritance in Python is only used if you want
to reuse some of the behavior of the base class.

2. Interfaces in Java are a result of not supporting multiple inheritance.
If you really need interfaces (for instance, you're rewriting a Java program
and you don't want to change anything), you can just use base classes.


>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090115/85d053e8/attachment.html>


More information about the Python-list mailing list