Protocols in Python

Ian Kelly ian.g.kelly at gmail.com
Sat Aug 26 22:32:21 EDT 2017


On Sat, Aug 26, 2017 at 6:35 PM, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>   The "The Python Library Reference, Release 3.6.0" (LIB) says:
>
>       »it must support the sequence protocol (the
>       __getitem__() method with integer arguments
>       starting at 0).«.
>
>   But in the "The Python Language Reference, Release 3.6.0"
>   (LANG) this is called »old sequence iteration« or »old-style
>   iteration« with the (new) sequence protocol being augmented
>   by »__len__«.

I think you're confused. There is no "new" sequence protocol. The
sequence protocol is just the set of special methods that one might
use to implement a sequence. There have been methods added over time
(e.g. __reversed__ was added in 2.6) but there has been no major
overhaul as is suggested by the word "new".

Where the sequence protocol is sometimes contrasted is with the
iterator protocol in the context of iteration. Prior to PEP 234, only
sequences could be iterated over because at the time iteration was
implemented using the sequence protocol, specifically the __getitem__
method. PEP 234 added a new way to iterate using the iterator protocol
but left the sequence protocol itself unchanged. Hence, iteration
using the sequence protocol is "old", and iteration using the iterator
protocol is "new".

>   Then, LANG mentions a »find_spec() protocol« and one can only
>   /guess/ that this is what is being described in »5.3.4 The
>   meta path«, because there it does not say "this is now the
>   definition of the find_spec() protocol here".

That wording could stand to be tightened up. Muddying the waters
further, 5.5.2 describes the "path entry finder protocol" which
involves implementing a *different* find_spec method.



More information about the Python-list mailing list