Python does a lot with magic methods and most of these are part of some protocol. I am familiar with the "iterator protocol" and the "number protocol" but recently stumbled over the term "sequence protocol". But even after some research I'm not exactly sure what the "sequence protocol" is.
For example the C API function PySequence_Check
checks (according to the documentation) if some object implements the "sequence protocol". The source code indicates that this is a class that's not a dict but implements a __getitem__
method which is roughly identical to what the documentation on iter
also states:
[...]must support the sequence protocol (the
__getitem__()
method with integer arguments starting at 0).[...]
But the requirement to start with 0
isn't something that's "implemented" in PySequence_Check
.
Then there is also the collections.abc.Sequence
type, which basically says the instance has to implement __reversed__
, __contains__
, __iter__
and __len__
.
But by that definition a class implementing the "sequence protocol" isn't necessarily a Sequence, for example the "data model" and the abstract class guarantee that a sequence has a length. But a class just implementing __getitem__
(passing the PySequence_Check
) throws an exception when using len(an_instance_of_that_class)
.
Could someone please clarify for me the difference between a sequence and the sequence protocol (if there's a definition for the protocol besides reading the source code) and when to use which definition?