I would expect that if I write a class that implements all methods needed for a Sequence as per https://docs.python.org/3.10/library/collections.abc.html, that Python would use duck-typing to know that an instance of it is a Sequence, but I find that is not the case:
from collections.abc import Sequence
class MySequence:
def __getitem__(self, ix):
pass
def __iter__(self):
pass
def __len__(self):
pass
def __contains__(self, val):
pass
def __reversed__(self):
pass
def count(self, val):
pass
def index(self, val):
pass
seq = MySequence()
print(isinstance(seq, Sequence))
This prints False instead of True. Why?
No duck-typing here. If you're not inheriting from the
Sequenceabc at all, then you'll need to register your class:From the docs point 3) concerning duck-typing: