Python 3.3+collectionsterm
collections.abc.Iterator
collections.abc — protocols
The iterator protocol: has __next__ and __iter__.
Syntax
isinstance(obj, collections.abc.Iterator)
Every Iterator is also Iterable, but not the other way round: a list can be looped over yet is not an iterator; iter(list) is. The practical value of the check is spotting single-use input — once drained, an iterator yields nothing on a second loop, so a function that needs two passes should materialise its argument into a list first.
Examples
import collections.abc as abc
print(isinstance(iter([1]), abc.Iterator)) # → True
print(isinstance([1, 2], abc.Iterator)) # → False
print(isinstance([1, 2], abc.Iterable)) # → True
print(isinstance(map(str, [1, 2]), abc.Iterator)) # → True
it = iter([1])
print(next(it, 'stop'), next(it, 'stop')) # → 1 stop
See also
Open in the full glossarydocs.python.orgReport an inaccuracy