all Python 3inspectfunction
inspect.isgeneratorfunction()
coroutines and generators
Checks that an object is a generator function: a function containing yield whose call returns a generator, not a result.
Syntax
inspect.isgeneratorfunction(object) -> bool
Syntax decides, not behaviour: a function that returns a generator expression or iter(...) is not a generator function even if it acts like one. An async generator (async def with yield) is not one either; use isasyncgenfunction. A function wrapped in functools.partial is recognized (since 3.8). It is easy to call a generator function for its side effect by mistake, and then its body never runs.
Examples
import inspect
def countdown(n):
while n:
yield n
n -= 1
def squares(n):
return (x * x for x in range(n))
print(inspect.isgeneratorfunction(countdown)) # → True
print(inspect.isgeneratorfunction(squares)) # → False — yield в теле нет
print(inspect.isgeneratorfunction(countdown(3))) # → False — это уже генератор
See also
Open in the full glossarydocs.python.orgReport an inaccuracy