Python 3.6+collectionsterm
collections.abc.Collection
collections.abc — protocols
The combination of Sized + Iterable + Container (a typical container).
Syntax
isinstance(obj, collections.abc.Collection)
The check is structural: any class defining __len__, __iter__ and __contains__ counts as a Collection, with no inheritance or registration involved. Two traps follow — str is a Collection too, so "a collection but not a string" needs an explicit carve-out, and generators or other iterators are not, since they have no len(). Use it as an annotation when you need size and iteration but not indexing; if order and indexing matter, reach for Sequence instead.
Examples
import collections.abc as abc
print(isinstance([1], abc.Collection)) # → True
print(isinstance({'a': 1}, abc.Collection)) # → True
print(isinstance('abc', abc.Collection)) # → True
print(isinstance((x for x in [1]), abc.Collection)) # → False
print(issubclass(abc.Collection, abc.Sized)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy