[Types-sig] Sample declarations

Samuele Pedroni Samuele Pedroni <pedroni@inf.ethz.ch>
Tue, 13 Mar 2001 16:14:28 +0100 (MET)


> - But note again that Sequence-ness and Mapping-ness are ill-defined.
>   Look at this:
>     def C:
>         def __getitem__(self, x): return x
>     x = C()
>   Is x a sequence?  A mapping?  It could fake being either!  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).  Does it need __len__?  Does it need __getslice__?  Does
>   it need to have count() and index()?  reverse() and sort()?  You
>   also need a separate type to indicate a writable sequence.  For
>   mappings, there's a whole slew of methods that are informally part
>   of the mapping protocol yet aren't implemented by all mappings,
>   e.g. has_key(), keys(), values(), items(), get(), copy(), and for
>   mutable mappings, clear(), setdefault(), update().
> - Similar questions can be asked about streams.
> 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.  That is, unless you make the
> type assertions only deal with the concrete types.  But that wouldn't
> be very useful, as there are lots of cases where you want to express
> that you accept any kind of sequence, mapping, file, etc.

(My external viewpoint)
The point is that until now python let one be lazy and free.
Clearly we can introduce a hierarchy of so to say abstract classes,
that at least mention all the methods that a class should implement
to be a Mapping, a Sequence, ...
But my feeling is that at the moment python is more like Self or Smalltalk
an input is fine for an algorithm as long it defines just the required
"selectors" (methods) in a meaningful way.
Is that a bad practice wrt to err&doc? probably yes but allows for
flexibility and quick coding.
If for example the whole std lib will introduce typechecking that
could be a problem.

regards, Samuele Pedroni.