[Compiler-sig] Polymorphism/encapsulation in Python

Rafael H Schloming rhs@MIT.EDU
Tue, 04 Apr 2000 15:38:44 -0400


> 	Object orientation is what makes
> Java counter-intuitive. Python, in many ways, is NOT restricted by
> a static type system, and therefore not restricted by a broken
> one like Java's. Instead, python programming is based on notions of
> protocols, such as 'sequence', where a mix of lightweight functional,
> procedural, and object oriented programming techniques coupled with
> dynamic typing, provide highly flexible notions of interfaces,
> not represented in the language (as they are in Java, Eiffel,
> and typically in C++ as well).

What exactly is the difference between protocals and interfaces? I
always looked at python as a language that makes very heavy use of
interfaces and therefore just about any code you write to operate on
lists or strings ends up working on anything satisfying the sequence
interface. Personally I think making this explicit would be a great
idea.

> 	One would hope for a decent static type system to be invented
> which can assist in typing such a protocol driven system,
> but it is not easy, and Python programmers prefer to live with notional
> interfaces and documentation. Note that a synthesis involving optional
> static typing is being worked on. 

I've written large scale software in python that had very complicated
class heirarchies with litteraly thousands of different classes. I
think python scaled very well to this level and I would choose it over
any other language, but what I found myself doing was putting in an
assert for every single argument in order to catch little slips like
swapping two arguments or passing in the wrong value somewhere.
Without these asserts the system would have been difficult to debug
because the errors wouldn't show up until very far beyond the place
where the actual bug occured. An interface mechanism would have
allowed me to express my asserts much more tersely and would have made
my code far more generic since I would be asserting things like
arg_foo satisfies interface bar instead of isinstance(arg_foo, ClassBar)
where ClassBar is just a specific implementation of interface bar.

--Rafael H. Schloming <rhs@mit.edu>