classes and interfaces

Paul McGuire ptmcg at austin.rr._bogus_.com
Tue Jun 27 09:18:03 EDT 2006


"Paul McGuire" <ptmcg at austin.rr._bogus_.com> wrote in message
news:ZO9og.26787$JW5.1066 at tornado.texas.rr.com...
>
> Lastly, you should look into
>

... this blog post:  http://dirtsimple.org/2004/12/python-is-not-java.html,
and its partner http://dirtsimple.org/2004/12/python-is-not-java.html.

Unfortunately, many who ask "How do I implement a Java interface in Python?"
are met with some defensiveness on this list, as if the OP is asking "What's
wrong with stupid Python language that it doesn't have something as basic as
interfaces?".  Sometimes this is attitude on the OP's part, sometimes just
presumption on the part of the various readers.  Some responses are of an
indignant "Interfaces? We don't need no stinkin' interfaces!" variety -
unfortunately, most of these shed more heat than light to the discussion,
usually omitting the important details as to *why* Python don't need those
malodorous code devices.

Java uses interfaces for two purposes:
1. to enforce at compile time whether a particular class implements a set of
methods, and correspondingly enforce that only instances of such classes are
permitted as arguments to functions/methods that declare arguments to be of
the interface type
2. as a workaround for not directly supporting multiple inheritance

Informally (and in design practices such as UML class diagrams), interfaces
also serve to document expected class capabilities - which in the case of
Java and C++, have the added support of compile-time checking to verify the
class contains methods corresponding to those in the interface.  Note that
I've been careful *not* to imply that Java classes that pass this
compile-time check actually *implement* the interface, as is often assumed.
The only thing one knows for sure when Java accepts a class as being an
interface implementer is that the class provides all the corresponding
method definitions - but it cannot know if all expectations of behavior and
pre- and post-conditions are actually implemented within those methods.
While compile-time checking is a decent crutch 95% of the time, it will
never catch errors like this:

    int passBackSomethingImportant() {
        // be sure to come back and implement this before production
release!
        return 0;
    }

Very few "how do I implement Java interfaces in Python" posters ever get
specific about which purpose they are trying to address with their Python
interface renditions.  It is entirely possible that the posters don't *know*
why the interface is there, they are just trying to directly port some Java
code to Python.

So specifically:
1. Python does not do any compile-time enforcement of method implementations
in classes of objects.  While some think this is like trapeze flying without
a net, Pythoneers will reply that the net is a false assurance anyway (based
on the example I show above).  So to "port" this type of interface, the only
thing to do is, well, ignore the interface - Python really doesn't require
it.  You *could* create a class such as:

    class ISomethingOrOther:
       def whatSomethingsDo(blah, blah2):
           raise NotImplementedError, "derived class failed to implement
'whatSomethingsDo' method"

and then have target classes include ISomethingOrOther in their base class
lists.  But Python does not do any compile-time enforcement with these
definitions.  You *still* have to do runtime testing to see if the
NotImplemented exceptions get thrown.

2. Python supports multiple inheritance without the indirection of
interfaces, so you can just inherit implementation classes directly:

class Swimmer:
  def swim():
     pass

class Flyer:
  def fly():
      pass

class Duck(Swimmer,Flyer):
   pass

and merrily write:

dd = Duck()
dd.fly()
dd.swim()


Sorry for the long discourse, I didn't have time to make it shorter.

HTH,
-- Paul





More information about the Python-list mailing list