Help understand why numpy array is not a Sequence type object.
from typing import Sequence
import numpy as np
x = np.array([1,2,3])
isinstance(x, Sequence)
---
False
An iterable which supports efficient element access using integer indices via the
__getitem__()special method and defines a__len__()method that returns the length of the sequence. Some built-in sequence types are list, str, tuple, and bytes. Note that dict also supports__getitem__()and__len__(), but is considered a mapping rather than a sequence because the lookups use arbitrary immutable keys rather than integers.The collections.abc.Sequence abstract base class defines a much richer interface that goes beyond just
__getitem__()and__len__(), adding count(), index(),__contains__(), and__reversed__().
Because it doesn't conform to any of the collections.abc interfaces, so specifically, collections.abc.Sequence should support index and .count. collections.abc.MutableSequence require that as well, but also __delitem__ and .insert
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With