In a duck typing world, this is a difficult question.
Both sequences and mapping use __getitem__()
to access items, using inter indexes and keys, respectively. Looking for the availability of the __getitem__()
method does not tell them apart, you need to look at what the method actually does.
For the dict it is not possible to know whether the integer argument to __getitem__()
is an index or a key, so it always works mapping-style.
Therefore, I think a dict is not a sequence at all, even though it supports iteration. Same applies to the set.
Looking at the collections.abc.Sequence
base class may be the best test. For custom types, just make sure they are derived from this base class.