The yield Keyword in Python

This article avoids heavy jargon and introduces the topic in plain language. If you have any hardware development experience, you may already find the yield keyword familiar — it behaves much like an interrupt on a microcontroller.


1. What is yield?

As a software developer you are probably well acquainted with return: it terminates a function and passes back a value. yield does something similar — it suspends a function and passes back a value. Note the word suspends; that is the key difference. You can resume execution right where it left off (i.e. at the yield statement) by calling the next method. Sound a lot like a hardware interrupt?

A function that contains yield can also serve as a generator — it produces an iterable object that you can iterate over with an iterator.

2. Why does yield exist?

Imagine you are eating when suddenly your phone rings — it is your boss calling. You stop eating, take the call, and then resume eating afterward. The call has a higher priority than eating, so you respond immediately. That is essentially an interrupt.

graph TB subgraph Function a[Eating] -->|Nothing happens| b[Eating] end subgraph Interrupt source a -->|Phone rings| c[Take call] -->|Call ends| b end

In programming, this pattern is called a coroutine. During function execution, when something requires waiting or has a higher priority, the current execution is paused, the other work is completed, and then the function continues.

Consider another scenario: a function that keeps computing and returning results. You could collect the results in a list and return it, but if the number of computations is enormous the list grows very large and consumes a lot of memory.

With yield, each result is returned one at a time. Iterating over the generator consumes far less memory and is more flexible to use.

3. Why should you care about yield?

Starting with Python 3.5, the asynchronous coroutine features in asyncio gained the new async / await syntax — a cleaner sugar coating that makes asynchronous code much more readable. Although yield is used less often now, there is a big difference between “knowing but not using” and “not knowing at all.” It is still worth understanding.

References