Generators · Lecture 01 · 15 min
Yielding and Generators
A function that contains yield returns a generator, an iterator that runs the function body lazily, pausing at each yield and resuming on the next call to next().
The hand-written way
Let’s start with a small iterator built the way you already know: a class with __iter__ and __next__. It produces the factorials 0!, 1!, 2!, … up to (but not including) n!.
import math
class FactIter:
def __init__(self, n):
self.n = n
self.i = 0
def __iter__(self):
return self
def __next__(self):
if self.i >= self.n:
raise StopIteration
result = math.factorial(self.i)
self.i += 1
return result
for num in FactIter(5):
print(num)It works, but look at how much of the code is bookkeeping. The interesting part is a single line, math.factorial(self.i). Everything else exists only to remember where we are between calls: the counter self.i has to be stored on the instance, because __next__ starts from scratch every time it’s called.
There’s a second way you’ve seen: the two-argument form iter(callable, sentinel). It calls a zero-argument function over and over and stops as soon as the function returns the sentinel value. A closure can hold the counter:
def fact():
i = 0
def inner():
nonlocal i
result = math.factorial(i)
i += 1
return result
return inner
# stop when we reach 5! (so we get 0! ... 4!)
for num in iter(fact(), math.factorial(5)):
print(num)Less code, but harder to read: you need to know the sentinel in advance, and the state is hidden inside a closure. What we’d really like is to write a plain loop and somehow pause it after each value.
yield: pause instead of return
That’s exactly what yield does. When a function body reaches yield value, it hands value back to the caller just like return would, but instead of throwing away the function’s state, Python suspends the function: the local variables and the current position in the code are kept. Later, the function can be resumed right after that yield.
The surprise is that you don’t resume it by calling the function again. Watch:
def my_func():
print('line 1')
yield 'Flying'
print('line 2')
yield 'Circus'
my_func()Calling my_func() didn’t print anything. Because its body contains yield, my_func is a generator function: calling it doesn’t run the body at all. It just creates and returns a generator object, which is ready to run the body when asked.
You ask with next():
gen = my_func()
print(next(gen))The body ran up to the first yield, printing line 1 along the way, and next returned the yielded value 'Flying'. The function is now frozen at that yield. Call next again and it picks up from there:
print(next(gen))And once more, with nothing left to run:
try:
next(gen)
except StopIteration:
print('StopIteration')next(), values one at a time, and StopIteration at the end: that’s the iterator protocol. A generator is an iterator.
next(). When the body finishes, the generator raises StopIteration.Generators implement the iterator protocol
If a generator is an iterator, it should have both __iter__ and __next__, and iter() should hand back the very same object:
gen = my_func()
print('__iter__' in dir(gen), '__next__' in dir(gen))
print(iter(gen) is gen)So everything that accepts an iterator accepts a generator: for loops, list(), sum(), zip(), unpacking, and so on. Python built the iterator for you from the function body.
How a generator ends: return
The generator above stopped because its body ran out of code. Remember that a function that falls off its end implicitly returns None. In a generator, returning (explicitly or implicitly) is what ends the iteration: Python raises StopIteration for you. If you return a value, it’s attached to the exception:
def squares(sentinel):
i = 0
while True:
if i < sentinel:
yield i ** 2
i += 1 # runs when the generator is resumed
else:
return 'all done!'
sq = squares(3)
print(next(sq), next(sq), next(sq))
try:
next(sq)
except StopIteration as ex:
print('StopIteration value:', repr(ex.value))Notice the i += 1 placed after the yield. That line doesn’t run when the value is produced; it runs the next time someone calls next(sq), as the very first thing the resumed body does. Thinking “the function freezes on the yield line” makes code like this easy to read.
A for loop silently swallows the StopIteration, and with it the return value. You’ll mostly see return values used with yield from (the last lesson of this section) and with generator-based coroutines later on.
for num in squares(5):
print(num)The factorial iterator, rewritten
Now compare the generator version of our factorial iterator with the class we started with:
def factorials(n):
for i in range(n):
yield math.factorial(i)
for num in factorials(5):
print(num)Three lines, and the state (i, n) is just ordinary local variables. The generator keeps them alive between calls, so there’s nothing to store on self and no StopIteration to raise by hand.
Every generator is an iterator, but not every iterator is a generator. FactIter instances are iterators that aren’t generators, just as every sequence is iterable but not every iterable is a sequence.
Generators get exhausted
Because a generator is an iterator, it can be consumed only once. After that it stays finished:
facts = factorials(5)
print(list(facts))
print(list(facts)) # nothing left
try:
next(facts)
except StopIteration:
print('still exhausted')To iterate again you call the generator function again to get a new generator. In two lessons you’ll see how to package that up in an iterable class so users don’t have to think about it.
You can inspect where a generator is in its life with inspect.getgeneratorstate:
import inspect
g = factorials(2)
print(inspect.getgeneratorstate(g))
next(g)
print(inspect.getgeneratorstate(g))
list(g)
print(inspect.getgeneratorstate(g))Since Python 3.7 (PEP 479), if a StopIteration escapes from inside a generator’s body, for example from a bare next() on some other exhausted iterator, Python converts it into a RuntimeError. Older code sometimes used raise StopIteration to end a generator early; today you simply return.
def bad():
yield 1
raise StopIteration # wrong way to stop
try:
list(bad())
except RuntimeError as ex:
print('RuntimeError:', ex)The change exists because a stray StopIteration used to end the loop silently, hiding real bugs.
Key takeaways
- A function whose body contains
yieldis a generator function. Calling it runs nothing; it returns a generator object. next(gen)runs the body until the nextyield, returns the yielded value and suspends the function with all its local state.- Generators are iterators: they implement
__iter__(returning themselves) and__next__. - When the body finishes or hits
return, the generator raisesStopIteration; a returned value ends up inStopIteration.value. - Like any iterator, a generator is exhausted after one pass. Call the generator function again for a fresh one.
- End a generator with
return, neverraise StopIteration(that becomes aRuntimeErrorsince 3.7).