[Types-sig] Protocols

Michel Pelletier michel@digicool.com
Tue, 13 Mar 2001 21:53:04 -0800 (PST)


On Tue, 13 Mar 2001, Paul Prescod wrote:

> I agree with Tim that there is no requirement to get the protocols a
> hundred percent right. If we make them "too heavyweight" then people
> will just choose to not implement all of the methods or will inherit
> from UserList to get some for free. Code that depends on those methods
> will crash just as it does today.

This is what I think about what thinking about bringing types to python:
to me, protocol means the same thing as interface.  Is this off-base?

> If we make them "too lightweight", code will depend on methods not
> strictly required by the interface and when those methods are missing,
> it will crash just as it does today.
>
> So code still crashes at runtime. That's the cost of a dynamically typed
> language. BUT we can still help people to avoid passing dictionaries
> (mappings) for lists (sequences) and vice versa and that is still better
> than nothing.

Agreed.

> The minimalist spirit is to lean towards "too lightweight." Therefore I
> propose that the protocol for mappings require only __getattr__,
> .keys(), .values() and .items(). The protocol for sequences requires
> only __getattr__.

Really?  Defining a sequence as getattr seems to broad to me.  To me,
something that only implements getattr is an object.  Of course, this
doesn't work for types, which are objects.  I would think a sequence was
defined by __len__ and __getitem__.

> If someone wants to make a richer set of features on
> THEIR sequences or mappings, they can define their own types as Tim
> mentioned. Those can be subtypes of the basics we define.
>
> In the future the system will stop guessing at the types of things based
> on their methods. If they don't have an actual interface declaration
> saying that they are trying to emulate a mapping or sequence (or
> whatever), we'll issue a warning that Python doesn't like to guess their
> intentions.
>
> In the meantime, we will guess a little. If we see only __getattr__,
> we'll presume a sequence.

We've made a couple of guesses along these lines, should I post them for
discussion?

-Michel