Summary: In this tutorial, you will learn the significance of yield keyword in Python with the help of examples.

Yield in Python

The yield keyword is used in the body of a function like a return statement, but instead of returning a value and terminating the function, yield produces a value and suspends the function’s execution, allowing the function to be resumed later on.

This behavior is known as “generator” function, as it generates a sequence of values over time, rather than computing them all at once and returning them in a list, for example.

Here is an example of a generator function that generates a sequence of numbers:

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

for number in count_up_to(5):

# Output:
# 1
# 2
# 3
# 4
# 5

In this example, the count_up_to() function is a generator that produces a sequence of numbers from 1 to max. The for loop iterates over the generator, printing each number as it goes.

When the generator function is called, it does not execute the function body immediately.

Instead, it returns a generator object that can be used to execute the function body later on. Each time the generator is iterated over, it executes the function body from the last yield statement up to the next one, producing a new value.

When the generator function terminates, the generator object raises a StopIteration exception to signal that the iteration is complete.

How to use StopIteration Exception?

The StopIteration exception is typically used to signal the end of an iteration over a generator. It is raised automatically by a generator when the generator function terminates, or it can be raised manually by the generator function to terminate the iteration early.

Here is an example of a generator function that uses StopIteration to terminate the iteration early:

def infinite_numbers():
    count = 0
    while True:
        count += 1
        if count > 10:
            raise StopIteration
        yield count

for number in infinite_numbers():

# Output:
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10

In this example, the infinite_numbers() generator function produces an infinite sequence of numbers. However, the generator includes a check to raise StopIteration after the tenth number.

This causes the for loop to terminate, as it is no longer able to produce new values from the generator.

It is important to note that the StopIteration exception should only be raised by a generator function or by code that is iterating over a generator. Raising StopIteration in other contexts can lead to unexpected behavior.

When to use yield?

You can use the yield keyword to create a generator when you want to produce a sequence of values over time, rather than computing them all at once and returning them in a list, for example.

This can be useful in situations where the full sequence of values would not fit in memory, or when the computation of the values is expensive and you want to avoid computing unnecessary values.

Here are some other reasons to use generators:

  • To iterate over a large dataset without reading it all into memory at once.
  • To perform a task that produces a large amount of output that you don’t need to keep around after the task is complete.
  • To produce a sequence of results that can be computed on demand, as needed.

Generators can also be more efficient than using lists in some cases, because they allow you to produce values one at a time, rather than creating an entire list of values in memory at once.

Here is an example of a generator function that reads a large log file and processes it line by line:

def process_log_file(filepath):
    with open(filepath, 'r') as file:
        for line in file:
            yield process_line(line)

for processed_line in process_log_file('large_log.txt'):

In this example, the process_log_file() generator function reads the log file line by line and processes each line using the process_line() function.

The generator function does not read the entire file into memory at once, but rather reads and processes one line at a time as the for loop iterates over it.

This allows the generator to efficiently process very large log files without consuming a lot of memory.

Adarsh Kumar

I am an engineer by education and writer by passion. I started this blog to share my little programming wisdom with other programmers out there. Hope it helps you.

Leave a Reply