How to check if a function is a generator or not in Python

1 Answer

0 votes
import types

def f1(x):
    yield x
        
def f2(x):
    return x

def add(x, y):
    return x + y

print(isinstance(f1(4), types.GeneratorType))
print(isinstance(f2(6), types.GeneratorType))
print(isinstance(add(7, 9), types.GeneratorType))



'''
run:

True
False
False

'''

 



answered Aug 29, 2021 by avibootz
...