[Types-sig] Sample declarations
Tim Peters
tim.one@home.com
Tue, 13 Mar 2001 20:31:41 -0500
[Guido, rediscovers that "sequence" and "mapping" etc are ill-defined]
> ...
> Before you can meaningfully discuss the Sequence check code, you
> should make up your mind when you start calling something a
> sequence. Clearly it needs __getitem__ (let's limit ourselves to
> class instances).
Not clear to me: the instant the new iterator protocol gets added to the
language, over half my "sequences" throw away their artificial __getitem__
methods, despite that they would work fine in oodles of code that expects "a
sequence".
def sum(seq):
sum = 0L
for s in seq:
sum += s
return sum
That should work dandy to add up the ints attached to the nodes of my (say)
binary tree "seq", and a binary tree has no natural need to implement
__getitem__.
> ...
> In short, it looks (and I am as surprised as you are!!!) like we will
> need to agree on the Python type hierarchy before we can ever hope to
> agree on any kind of type assertions.
Were you on vacation the last two times this SIG was alive <wink>? We've
never managed to agree on useful definitions for Python's "folklore
protocols" -- and that wasn't for lack of trying. Old news: adding type
checks subtracts from flexibility, unless the type checks are at the level of
individual operations (Getitem_able, TwoIndexSlice_able, Has_key_able,
Iter_able, etc), in which case type checks can add enormous tedium, and/or
enormous confusion as N programmers each group the lowest-level capabilities
in their own unique ways.
But Paul shouldn't let even that stop him. While the folklore protocols are
ineffable this way, a great many useful typechecks in practice will be of
very simple isinstance(object, types.IntType) and isinstance(object,
SomeClassName) forms. For all the rest, we make up something more or less
arbitrary. A saving grace is that many applications of the folklore
protocols truly require only a single method, so that e.g. WriteString_able
is a perfectly adequate typecheck for the many routines that require only
.write(String) of a "file-like object".
panic-is-premature-albeit-prudent<wink>-ly y'rs - tim