Sitemap

Python Generator Functions Explained

3 min readAug 23, 2025

--

In 1966, Ole-Johan Dahl and Kristen Nygaard introduced the object-oriented paradigm. To emulate objects, they used a feature of ALGOL that allowed moving the function call stack frame into dynamic memory (the heap).

Press enter or click to view image in full size

Fast forward to 2001, Guido van Rossum “reinvented” objects in Python 2.2 by introducing generator functions.

What is a Generator Function?

A function becomes a generator when it contains the yield keyword:

"yield" [expression]

Unlike return, the yield statement doesn’t terminate the function. Instead, it produces a value and pauses execution, saving the state of local variables and the call context.

Get Vitaly Sem’s stories in your inbox

Join Medium for free to get updates from this writer.

This is similar to how ALGOL kept stack frames in the heap.

Calling a Generator Function

Example:

def generator_function():
print("begin")
yield

Calling it:

generator_instance = generator_function()

print(generator_instance)
# <generator object generator_function at 0x...>

type(generator_instance)
# <class 'generator'>

Notice that "begin" wasn’t printed yet — the function body doesn’t execute until iteration starts.

Generators Are Iterators

iter(generator_instance) is generator_instance
# True

item = next(generator_instance)
# Output:
# begin

A generator is an iterator, meaning it works with iter() and next(). Execution resumes from the last yield point until the next yield is hit.

Iterating a Generator

Let’s expand our example:

def generator_function():
print("begin")
for i in range(3):
print("iteration #", i)
yield i
print("post yield")
print("end")

Usage:

gen = generator_function()
print(next(gen))

# begin
# iteration # 0
# 0

print(next(gen))
# post yield
# iteration # 1
# 1

print(next(gen))
# post yield
# iteration # 2
# 2

print(next(gen))
# post yield
# end
# StopIteration

When the generator finishes, Python raises StopIteration. Loops like for automatically handle this exception.

Using Generators in Contexts

Generators can be used anywhere an iterator is expected.

With a for loop:

for i in generator_function():
print("item", i)

Converting to a list:

list(generator_function())
# [0, 1, 2]

Generators with Parameters

They can also accept arguments:

def digits(string):
for ch in string:
if ch.isdecimal():
yield ch


list(digits("Mar 21"))
# ['2', '1']

Yielding None

If you write yield without an expression, it produces None:

def digits(string):
for ch in string:
if ch.isdecimal():
yield ch
else:
yield


list(digits("Mar 21"))
# [None, None, None, None, '2', '1']

return Inside a Generator

A return statement ends the generator and attaches its value to the StopIteration exception.

def generator_function(limit):
for i in range(limit):
yield i
if i % 4 == 3:
return "That's all Folks"

Example:

gen_long = generator_function(100)

try:
while True:
print("yield", next(gen_long))
except StopIteration as error:
print("return", repr(error.value))

# yield 0
# yield 1
# yield 2
# yield 3
# return "That's all Folks"

If the function ends normally, the return value is None:

gen_short = generator_function(3)

try:
while True:
print("yield", next(gen_short))
except StopIteration as error:
print("return", repr(error.value))

# yield 0
# yield 1
# yield 2
# return None

Key Takeaways

  • A generator function is defined like a normal function but contains yield.
  • Calling it returns a generator object, which is both iterable and an iterator.
  • Generators save state between iterations, making them memory-efficient for large or infinite sequences.
  • They can be used with for, in, list(), and other iteration contexts.
  • A return inside a generator signals completion and attaches a value to the StopIteration exception.

This was just the beginning. In the next part, we’ll explore advanced generator methods: send(), throw(), and close().

--

--