[Python-3000] my take on "typeclasses"

Guido van Rossum guido at python.org
Thu May 11 20:19:43 CEST 2006

On 5/11/06, Talin <talin at acm.org> wrote:
> The word 'sequence' here doesn't mean an exclusive category. In a sense,
> all sequences are really mappings - they "map" a range of integers to
> values.

No, the requirements for sequence-ness are stronger. The set of ints
mapped must be exactly range(len(x)). And iterating over the sequence
is required to return the values in numerical order of the index set
(unlike iterating over a dict whose keys happen to be a contiguous set
of ints starting at 0). And for mutable sequences, insertions and
deletions move all the other keys around -- very different from a
mutable mapping. And sequences are expected to implement slicing,
concatenation and repetition.

In general, there are two possible taxonomies of container types for
Python: minimal and maximal. The minimal taxonomy attempts to describe
the minimal features that a  specimen of container ought to have in
order to be considered a member of a given species of container. It is
useful for determination but not so useful for prediction of behavior.
The maximal taxonomy attempts to describe all the features shared by
(most) members of a species; it's harder to use for determination, but
more useful for describing expected behavior.

I think I'm more comfortable with a maximal taxonomy. In a maximal
taxonomy, I'd describe a large set of invariants, attributes,
behavior, etc., and say e.g. "this is how a file behaves". A
particular class can then claim to be a file by explicitly declaring
this (how that's spelled is a different issue -- it doesn't need to be
done by inheritance from an abstract base class or interface, it could
also be an arbitrary property, agreed upon by convention, or an
external registry of all file types, for example).

For the purposes of generic/overloaded functions, if we can overload
on such abstract container species, the declared container species (or
the "best" one, if multiple species can be declared) should determine
the implementation chosen -- and damn the torpedoes if the object
declares it's a file but doesn't implement isatty(). (I.e. I don't
care, it's still a file, just like a three-legged dog is still a dog.)

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-3000 mailing list