Skip to content

How to Check If a Function Is a Generator in Python

Learn how to programmatically identify Python generator functions using inspect.isgeneratorfunction() and check generator objects with inspect.isgenerator() for memory-efficient iteration.

In Python, generators are a special category of functions that allow you to declare a function that behaves like an iterator. Instead of returning a single value and exiting, they allow you to yield a series of values over time, pausing execution between each one. This makes them highly memory-efficient for large datasets.

However, because generators are defined using the standard def keyword, it is not always immediately obvious if a function is a standard function or a generator. This guide explains how to programmatically distinguish between the two.

Understanding Functions vs. Generators

Before checking types, it is important to understand the behavioral difference.

  • Regular Function: Runs to completion and returns a single result (which could be a list). It holds all data in memory.
  • Generator Function: Returns a "generator iterator" immediately. It executes code only when values are requested (lazy evaluation).
def get_squares_list(numbers):
    result = []
    for n in numbers:
        result.append(n * n)
    return result

def get_squares_gen(numbers):
    for n in numbers:
        yield n * n

nums = [1, 2, 3]
print(f"Function returns:  {type(get_squares_list(nums))}")
print(f"Generator returns: {type(get_squares_gen(nums))}")

Output:

Function returns:  <class 'list'>
Generator returns: <class 'generator'>

If you have the function object itself (the definition) and want to know if it will behave as a generator when called, the standard inspect module provides the most robust tool.

inspect.isgeneratorfunction(obj) returns True if the object is a Python generator function.

import inspect

def my_regular_func():
    return "Hello"

def my_generator_func():
    yield "Hello"

is_reg_gen = inspect.isgeneratorfunction(my_regular_func)
is_gen_gen = inspect.isgeneratorfunction(my_generator_func)

print(f"Is 'my_regular_func' a generator?   {is_reg_gen}")
print(f"Is 'my_generator_func' a generator? {is_gen_gen}")

Output:

Is 'my_regular_func' a generator?   False
Is 'my_generator_func' a generator? True

Method 2: Checking the Result Object

Sometimes you don't have access to the function definition, but you have the result of a function call. You need to know if that result is a generator iterator.

For this, you use inspect.isgenerator().

import inspect

def number_gen():
    yield 1
    yield 2

gen_object = number_gen()

print(f"Is object a function? {inspect.isgeneratorfunction(gen_object)}")

print(f"Is object a generator? {inspect.isgenerator(gen_object)}")

Output:

Is object a function? False
Is object a generator? True

The Role of the yield Keyword

Syntactically, the defining characteristic of a generator in Python is the use of yield. If a function contains yield anywhere in its body, Python treats it as a generator.

import inspect

def return_list():
    return [1, 2, 3]

def yield_items():
    yield 1
    yield 2
    yield 3

print(f"Has yield? {inspect.isgeneratorfunction(yield_items)}")
print(f"Only return? {inspect.isgeneratorfunction(return_list)}")

Output:

Has yield? True
Only return? False

Conclusion

To determine if a function is a generator:

  1. Use inspect.isgeneratorfunction(func) if you are checking the function definition itself.
  2. Use inspect.isgenerator(obj) if you are checking the return value of a function call.
  3. Look for yield: Any function containing yield is automatically a generator.
Navigation