Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why numpy array is not of Sequence type?

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
  • sequence

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__().

like image 435
mon Avatar asked Aug 03 '26 06:08

mon


1 Answers

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

like image 55
juanpa.arrivillaga Avatar answered Aug 04 '26 20:08

juanpa.arrivillaga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!