On Tue, Dec 29, 2020 at 4:24 PM Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
The library reference doesn't seem to use the terms "sequence protocol"
and "mapping protocol". It talks about "sequence types" and "mapping
types", but doesn't use the word "protocol" in relation to them.

The only one I've really seen used fairly universally is the "iterator protocol" -- though in the docs, it's under "Iterator Types": https://docs.python.org/3/library/stdtypes.html?highlight=iterable#iterator-types

But then, under that: "The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:"

So that's (I think) the only place it documented and called a protocol.
Nor is there any clear list of methods that a type needs to provide
in order to be considered a sequence or mapping.

Isn't that what the ABCs are?

The trick here, in this context, is that something doesn't need to be a fully functioning Mapping to be unpacked.

But there are a handful of places where a subset of the Mapping API is needed (apparently .keys() and __getitem__, with a particular relationship). So It is good to (a) be consistent about that -- which I think is the case today, and (b) document it -- which is not the case.

Given that where the iteration protocol is described is under built in types, it makes sense to me to t put it there -- maybe call it the "mapping unpacking protocol" (better ideas welcome!).
There are lists of
operations supported by the built-in container types, but those are
fairly extensive, and it's not obvious which ones are vital.

there's more than that -- there's the ABCs. But anyway, the problem with "vital" is the core of duck typing -- what's vital depends on the context -- We can only (and should only) define that for a few places where they are used in core Python -- is there anything else other than Mapping unpacking that we should identify and document?
So are the "sequence protocol" and "mapping protocol" really mythical
beasts that don't really exist? Are they more akin to the infamous
"file-like object" which is whatever it needs to be for the purpose
at hand?

See above -- I don't think there's a Sequence protocol at all. And the "mapping protocol" that at least I"ve been talking about is in a particular context -- unpacking, or somewhat more generally, when you want to be able to access all the items in a Mapping, but not do anything else with it.

Guido has since said that the ABCs are intended to be definitive,
but the docs don't really make that clear either. (And the ABC doc page
talks about "APIs", not "protocols"!)

I think the ABCs Are definitive -- but what are they definitive of? They are certainly not minimal protocols.


Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython