[Types-sig] Sample declarations
Jeremy Hylton
jeremy@alum.mit.edu
Tue, 13 Mar 2001 12:54:03 -0500 (EST)
>>>>> "GvR" == Guido van Rossum <guido@digicool.com> writes:
GvR> In short, it looks (and I am as surprised as you are!!!) like
GvR> we will need to agree on the Python type hierarchy before we
GvR> can ever hope to agree on any kind of type assertions. That
GvR> is, unless you make the type assertions only deal with the
GvR> concrete types. But that wouldn't be very useful, as there are
GvR> lots of cases where you want to express that you accept any
GvR> kind of sequence, mapping, file, etc.
I noodled with an ML-style type inference engine for simple Python
functions. One of the problems I remember running into was that most
operators work for a variety of object types. If I see an expression
with a "+" operator, I can't infer a lot about the operands. Even if
I know one of the operands is an int, the other operand could be some
other kind of number. (I was explicitly ignore an instance with an
__add__ method.)
It's an interesting problem, because it forces us to consider the
documentation issue head on. If you write code that requirs a
file-like object to have only read and readlines, what name do you
give that interface? Other code might reasonably expect the file to
have read, seek, and tell. Etc.
We can write code quickly in Python in part because we don't have to
decide in advance which methods we want to require -- and we don't
have to implement every method that a builtin file has when we write a
class that is a file-like object.
If we want to add type declarations to argument lists, we need to
document exactly what the function requires or we need to pick a
useful set of standard types (supports __getitem__, mapping, mutable
mapping) and fix all the code that only implements part of a
particular type.
Jeremy