I YIELD!!!

What the frick is yield?

In short: yield is a keyword in Python used inside functions to turn them into generators.


Then what the heck is a generator?

A generator is like a function that can pause and resume its execution.

  • Instead of returning a single value and exiting (like return), yield produces a value and remembers where it left off.
  • You get the next value by calling next() on the generator.
  • In a for or while loop, Python automatically calls next() until there are no more values (raises StopIteration).

Why the duck should you use yield?

When you want to generate a sequence of values lazily (one at a time), especially useful for:

  • Large datasets
  • Streaming data
  • Infinite sequences

Benefits:

  • Saves memory (doesn't store everything in memory at once)
  • Can handle data as it comes in
  • More resilient for long-running processes

Example:

def count_up_to(n):
    count = 1
    while count <= n:
        yield count
        count += 1

gen = count_up_to(5)

for num in gen:
    print(num)

Output:

1
2
3
4
5

Each time yield is hit, it sends a value out and pauses. On the next iteration, it resumes right after the yield.

Quick comparison:

returnyield
Returns once and exitsCan produce multiple values
Produces a single valueProduces a generator
Cannot resumeRemembers state between calls

Common use cases:

  • Processing large files line-by-line
  • Streaming data from APIs
  • Infinite sequences (e.g. Fibonacci)
  • Data pipelines
  • Chunked processing

Real-world-ish example:

Imagine you're fetching data from an external source that supports pagination or streaming. Using yield allows you to process each chunk as it arrives, improving memory usage and resilience:

def fetch_data():
    results = []
    try:
        for chunk in fetch_data_stream():
            results.extend(chunk)
    except Exception as exc:
        # Handle any errors that occur during fetching
        pass
    return results

def fetch_data_stream():
    try:
        while True:
            # Do stuff to retrieve data, e.g. paginated API calls
            result = get_next_chunk()
            if not result:
                break
            yield result
    except Exception as exc:
        # Handle streaming-specific errors if needed
        pass

Here, if something fails mid-way, whatever data has already been yielded and processed is preserved — reducing the chance of losing large amounts of progress.