How do Java interfaces translate to Python?

brueckd at tbye.com brueckd at tbye.com
Sun Sep 30 23:24:02 EDT 2001


> I'm primarily a Java programmer but I dabble in Python on a regular basis.
> My current project is a low- and high-level interface to IRC which can be
> re-used to write IRC clients and bots.
>
> My question is this: Java being my primary language, I'm very used to
> using interfaces and I was wondering if there was an equivalent construct
> or idiom in Python? How does one do in Python what one would do with
> interfaces in Java?

Hi Gordon, I recently started porting about 60K lines of Java to Python
and so far the steps required to port interfaces go something like this:

1. Delete the Java interface file.

There is a group of Python programs for which you could determine, at
compile time, if a Python class implements a particular set of methods,
but Python is just too dynamic to make it work in the general case. For
example, at runtime you can add and/or remove methods to a class and/or an
instance, so realistically you won't know if an object implements a
particular interface until it's time to use it.

This doesn't mean that you can't think in terms of contracts between
objects and functions anymore, just that Python assumes you're either
honest and always abide by that contract, or that you're smarter than it
and know what you're doing (Java assumes you're both dishonest *and*
stupid).

-Dave





More information about the Python-list mailing list