Python 3.3+collectionsterm
collections.abc.MutableSequence
collections.abc — protocols
The mutable sequence protocol (list): adds append/insert/__setitem__.
Syntax
isinstance(obj, collections.abc.MutableSequence)
To build one yourself you supply __getitem__, __setitem__, __delitem__, __len__ and insert; append, extend, pop, remove, reverse and += come from the mixins. As a runtime check it is broader than isinstance(x, list) — bytearray and collections.deque qualify, while str and tuple do not, since they cannot be modified. As a parameter annotation it is more honest than list: it signals that the function intends to mutate what you pass in.
Examples
import collections.abc as abc
print(isinstance([], abc.MutableSequence)) # → True
print(isinstance((), abc.MutableSequence)) # → False
print(isinstance((), abc.Sequence)) # → True
print(isinstance('abc', abc.MutableSequence)) # → False
print(isinstance(bytearray(b'ab'), abc.MutableSequence)) # → True
lst = [1, 2]; lst.insert(0, 0); print(lst) # → [0, 1, 2]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy