What does the “yield” keyword do in Python?

For the purpose of generating a series of values in Python, the yield keyword is used within the context of a generator function. When the yield statement is included in a function, the function is transformed into a generator. Therefore, invoking the function that generates the function results in the return of an iterator.

As an illustration of how yield works, here is a simple example:

def my_generator():
    yield 1
    yield 2
    yield 3

# Using the generator
gen = my_generator()

# Iterating over the generator
for value in gen:
    print(value)

In this particular example, the my_generator function is a generator. If you call the my_generator() function, it will return an object that is a generator. To signify the places at which the generator will create a value and then halt its execution until the next value is requested, the yield statements that are included inside the function denote these locations.

The function is called until it comes across a yield statement, at which time it returns the value that was yielded. This occurs when you iterate over the generator by using a for loop or when you explicitly use next() on the generator object. On the following time that a value is requested, the function’s state is preserved, which enables it to restart execution from the point at where it was previously stopped.

This sluggish evaluation and suspension of state makes generators memory-efficient for processing big datasets or endless sequences. This is because generators output values one at a time on demand, which allows them to handle infinite sequences.

Consider the following illustration of an endless sequence:

def infinite_sequence():
    num = 0
    while True:
        yield num
        num += 1

# Using the generator to get the first 5 values
gen = infinite_sequence()
for _ in range(5):
    print(next(gen))

The infinite_sequence generator is responsible for producing an infinite sequence of numbers in this scenario; however, it only creates these numbers when they are required. Making it possible to deal with sequences that would be impossible to construct all at once is made possible by the yield statement, which enables the function to halt and restart its execution.

What is the difference between yield and return?


The yield keyword in Python is analogous to a return statement that is used for returning values in Python. However, rather of just returning a value, the yield keyword returns a generator object to the person who uses the function that includes yield. The most significant distinction between them is that the return statement is responsible for putting an end to the execution of the function. With the yield statement, on the other hand, the function’s execution is only temporarily halted. An other distinction is that return statements are never put into activity. yield statements, on the other hand, are really carried out once the function resumes its execution.

Potential benefits of yield:

As a result of the fact that the execution of the code only takes place when the caller iterates over the object, the yield keyword is very memory efficient.
Considering that the states of the variables are stored, we are able to halt and continue from the same position, which allows us to save time.
Some are the drawbacks of yield:

As a result of the function generator returning the same value several times, it might be difficult to comprehend the flow of the code at times.
In order to avoid problems in the software, it is essential that the procedures for calling generator functions be handled correctly.

Leave a comment