[Types-sig] A type checking system in Python
Paul Prescod
paulp@ActiveState.com
Wed, 21 Mar 2001 11:13:00 -0800
"hzhu@users.sourceforge.net" wrote:
>
> ...
>
> One solution promoted by some other languages is to define some abstract
> classes for type checking purpose, and require them to be inherited by any
> class that behaves like them. This is like what interfaces do in Java.
> This solution is not satisfactory because
>
> - It breaks polymorphism. One has to harness the property at the time the
> classes are defined, instead of when they are used. For example, if an
> application requires a property of "ReadableFile" which is satisfied by
> sys.stdin, it is difficult to retrofit the definition of sys.stdin to be
> an instance of such.
It is too strong to say it breaks polymorphism. It can in some cases
*hamper* polymporhism. But Java is a fundamentally polymorphic language,
as are all other object oriented languages I have used. Polymorphism is
the central point of OO. Even languages that do not support either
encapsulation or inheritance support polymorphism.
> - It mixes two incompatible inheritances. For example, suppose that class A
> has method A.m() but its subclass B overrides it with B.m(). The property
> A satisfies is not necessarily satisfied by B.
You cite Java interfaces above and yet Java interfaces are *specifically
designed* to separate out the two different kinds of inheritance. That's
why there are interfaces which are distict from classes!
> From the above it is clear that there is a useful conceptual hierarchy that
> is independent of the class hierarchy. For lack of a better name let us
> just call them Types.
We've been calling them interfaces and/or protocols. Python already has
a meaning for "type" which is somewhat confusing in this context.
> - classes embody implementations
> - Types embody requirements
This is exactly the distinction between Java classes and interfaces. I
have a prototype implementation that embodies this distinction here:
http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/python/nondist/sandbox/typecheck/?cvsroot=python
We've been discussing these issues in the type-sig. You are invited to
join us there.
--
Take a recipe. Leave a recipe.
Python Cookbook! http://www.activestate.com/pythoncookbook