liveBook · Manning

Yield

As explained in Python How-To

The yield keyword pauses a generator function, provides a value to its caller, and allows execution to resume later from the pause point.

Need to produce values one at a time instead of building a complete collection? Put yield inside a generator function. Each yield provides a value, pauses execution, and lets the generator resume later from the following line. section 7.4.1

Yield perfect squares lazily

def perfect_squares(limit): n = 1 while n <= limit: yield n * n n += 1 sum(perfect_squares(3)) # 14 Calling next repeatedly retrieves 1, 4, and 9. The generator remembers its state between calls, then becomes exhausted when the loop ends. section 7.4.1

Listing 7.10 Creating a generator to calculate the sum of perfect squares
def perfect_squares(limit):
    n = 1
    while n <= limit:
        yield n * n
        n += 1
 
squares_gen = perfect_squares(upper_limit)
 
sum_gen = sum(squares_gen)
 
assert sum_gen == sum_list == 333333833333500000

Inspect the complete generator that calculates the sum of perfect squares and compares it with an equivalent list-based result. section 7.4.1

Keep these usage distinctions in mind:

  • Use next(generator) to resume execution manually; iteration constructs call next behind the scenes. section 7.4.1
  • Treat yield differently from return: return ends execution, while yield temporarily gives control back to the caller. section 7.4.1
  • Remember that consumed values do not reappear; calling next three times before sum causes sum to begin with the next value. section 7.4.3
  • Prefer a generator expression when the generation logic is straightforward, using parentheses rather than square brackets. section 7.4.3

How Python How-To covers it

How deep it goes
Explains pausing, resumption, state retention, exhaustion, next, and StopIteration, while deferring coroutine details.
Examples
Uses perfect-square generators, manual next calls, generator expressions, and sums to demonstrate lazy production and consumption.
What it assumes
Assumes familiarity with functions, loops, iterators, and comprehensions while introducing generator behavior through examples.
What you can do after
Build it from scratch by writing generator functions that yield values incrementally and resume correctly across iteration.
How it connects
Connects yield with generators, iterators, next, StopIteration, generator expressions, return, and generator-based coroutines.

Also explained in

Compare how 10 books explain yield →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.