[Compiler-sig] Polymorphism/encapsulation in Python

Ludvig Svenonius dominio@hem.passagen.se
Tue, 04 Apr 2000 13:11:56 +0200


Many people have been singing their praises to the extreme level of
polymorphism found in Python. Having recently migrated from Java to
Python, however, I find myself wanting in the area. The main problem may
still be the lack of focus on encapsulation, however (and by
encapsulation I do not mean baby-sitting the programmer with access
modifiers like C++'s 'private').

Even as a Java programmer, I can certainly praise Python's support for
multiple inheritance. Java's all the less for not having it. However,
Java's facility for interfaces is a pure blessing, and I lack a
counterpart for it in Python.

As an example, I will bring up recent scenarios that I've been
confronted with when working with XML and the DOM using Java and Python.
For Java there exists dozens of libraries with support for both core and
peripheral XML standards like Namespaces, XPointer, XSLT, etc. The
advantage Java has over Python in the area is not so much the
availability of existing libraries, but their general applicability. W3C
defined their DOM standard for Java in the form of Java interfaces.
These interfaces became the standardized foundation for libraries that
depend on the DOM in Java. Because of this fact, that all (well, almost)
available DOM-based libraries for Java work against the same clearly
defined interface sets, as defined by W3C, it is trivial to write a new
DOM implementation and with ease provide it with the features it needs
to properly interact with previosly written libraried, based on the
standard DOM interface.

Unfortunately, doing the very same thing in Python is a hassle. There is
no clearly defined DOM interface in Python. Of course the standard DOM
interface as proposed by the W3C could be used, as it is in
implementations like 4DOM. The problem arises when a user needs to write
his/her own implementation of the DOM (or anything else that could have
a standardized interface) and needs to make this compatible with
existing libraries written for Python. To make my DOM compatible with
4DOM, perhaps I need to subclass the 4DOM classes, so I can use 4XPath
and 4XSLT with my DOM implementation, but to get my DOM implementation
to work with other libraries as well, other measures, depending on the
implementation of the library (which is something that according to the
OOP principle of encapsulation should be completely irrelevant for me as
a user of the library) will likely be necessary.

Java's interfaces were designed as a replacement for multiple
inheritance. The lack of support for multiple inheritance sometimes
makes design in Java grossly unintuitive, however. As I see it,
interfaces can, with great benefit, be combinated with multiple
inheritance. In such a design multiple inheritance would mainly be a
convenience feature, whereas polymorphism would supply the main support
for encapsulation/polymorphism with benefit like the ones found in Java.

Interfaces in Python could perhaps take the form of a facility which
enables the user to validate an instance against a pre-defined list of
functions that are expected to be found in the instance, raising an
exception in case of a mismatch. This would of course be possible to do
manually in any Python function, but that completely misses the point,
as the checking is then part of the function implementation, which the
user should not have to concern him-/herself with. The interface
facility would furthermore be quite useless unless standardized, so that
libraries that make use of the interface facility can start to crop up.

What I would suggest is looking into providing native support for
interfaces in Python to sidestep these problems. The current lack of it
seriously takes the edge out of available APIs and libraries written in
Python. Perhaps it could be accomodated in Python 1.7 or Python 3K.

I know for one thing that I'll never feel comfortable with leaving Java
fully behind me until it is in.

--
Ludvig Svenonius
Researcher, Excosoft AB
+46 (8) 752 74 29
ludvig.svenonius@excosoft.se   /   dominio@hem.passagen.se