Python 3.5+collectionsterm
collections.abc.Generator
collections.abc — protocols
The generator protocol: an iterator with send/throw/close.
Syntax
isinstance(obj, collections.abc.Generator)
A real generator passes — a function with yield or a generator expression — and so does any class that also defines send, throw and close; your own class with just __iter__ and __next__ registers as an Iterator but not as a Generator, since send, throw and close are required too. When you only mean "lazy one-shot iteration", check Iterator instead. And remember the trap generators themselves carry: once fully consumed, the object is spent, and a second loop over it silently yields nothing.
Examples
import collections.abc as abc
def g():
yield 1
print(isinstance(g(), abc.Generator)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy