interfaces?

Alex Martelli aleaxit at yahoo.com
Fri Feb 2 03:50:33 EST 2001


<drewpc at colorado.edu> wrote in message news:95ckj2$dlc$1 at nnrp1.deja.com...
> Let me preface this posting by saying that I'm not a fan of Java.  A lot
> of people may argue with me, but that's not the point.  The point is
> that I think that, one of the only useful features of Java is the idea
> of an interface.

It's a good idea, but COM did it better, before Java ever existed:-).

E.g., one think I love about COM interfaces vs Java's: a COM object
is *informed* when one of its interfaces is requested of it -- it
gets to 'intercept' the interface-request, if you will, and thus can
do whatever work it may need to prepare itself for "actually HAVING"
the interface it advertises -- acquire resources, etc; explicitness
of navigation between interfaces is just lovely -- as usual, "explicit
is better than implicit".

In Java, like in C++, the native protocol for "navigating between the
interfaces of a given object" is a cast (dynamic_cast, in C++), and
the object doesn't even get _informed_ that somebody's trying to cast
it in a such-and-such way.  So, when you need 'hooks' upon interface
navigation, you can't have them within the native protocol -- you
must redundantly check at each method-entry, or use other non-native
protocols whereby your "secondary interfaces" are requested by some
method-call.

It's a bit better in Python, since __getattr__ does provide a natural
place to hang a hook for interface-navigation -- whenever a method in
interface X is requested, and you don't yet have it in your dict, your
__getattr__ get called, and you can diagnose the situation, do whatever
you require to prepare to actually support interface X, and add X's
methods to your __dict__ (or switch your runtime class to add some
X implementation to its bases, etc) so your __getattr__ won't be
bothered again about them.  Still, it's somewhat dark magic in Python,
while it's clear as crystal in COM (if you understand COM's semantics
rules about identity &tc; Don Box's great "Essential COM" is highly
recommended to foster such understanding!-).


Anyway, "interfaces are a good concept" is a widely shared opinion,
among Pythonistas as well as among others -- including users of
other languages that don't model interfaces *explicitly*, but still
let you use them one way or another.  You could say of Python (and
Smalltalk, etc) that interfaces are there, just not 'bundled' --
each method-cum-signature (that you can conceptually group into an
interface) is on its own.  You could say of C++ (and Eiffel, etc)
that interfaces are there, just not named as such -- you can model
them, if you wish, by using special-cases of classes (with multiple
inheritance), and good C++/Eiffel designers do that a LOT -- see,
e.g., John Lakos' great book "Large Scale C++ Software Design"
(Addison-Wesley, 1996) -- what you and I and COM and Java call
"interfaces", Lakos calls "protocol classes", but, terminology
apart, the concept IS very much there, and it's developed solidly
and convincingly in the context of a splendid discussion of the
issues of large-scale software design (dependency management, &c).

(I can't mention Lakos without a nod to Robert Martin, who writes
about very similar concerns -- interfaces, components, dependency
managements -- in VERY clear ways, having a gift for wrapping up
the wisdom he imparts into easily digestible little morsels -- if
you can stomach reading a little bit of C++ code, about all of his
articles at http://www.objectmentor.com/publications/articlesByAuthor.html
are great reading, IMHO; Lakos' book is more organic and thorough,
but by that same token it requires a substantial investment of time
and energy to study it in depth and digest it as it deserves to
be -- Martin's articles, focusing on one concern at a time, may
prove much more palatable fare for most of us over-worked, chronically
short of time, short-attention-span, any-unbillable-minute-had-
BETTER-give-great-returns, typical software professionals).


Anyway, back to Python -- see http://www.prescod.net/pytypes/, and
http://www.zope.org/Wikis/Interfaces/FrontPage, for some of the
previous thoughts on interfaces in Pyhon.


Alex






More information about the Python-list mailing list