Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using return vs yield in Generator Functions

For more information on topics covered in this lesson, check out these resources:

00:00 In this lesson, we’ll explore what happens when you use a return statement in a generator function. A generator function is a function that produces a sequence of values.

00:11 This is done by using yield statements inside the function. A call to this function returned something referred to as a generator iterator.

00:22 So this type of function is something like a factory, not to be confused with the factory pattern we saw earlier. So we can think of this as a generator factory.

00:33 You can see the Real Python resources How to Use Generators and yield in Python and Python Generators 101 for more information on generator functions.

00:46 You can use return statements in generator functions to indicate that there is no more work for the function to do. When this happens during program execution, the function raises a StopIteration exception when it encounters a return statement.

01:02 The return value from the function will be used as the exception’s initializer argument and will be stored in its .value attribute. Here’s a simple generator that will produce 1 and 2 as requested, and then return 3 when finished To use it, we assign the function call to a variable, which becomes our generator object.

01:32 Then we use the function next() with our generator as an argument to retrieve the next item from that generator. After using all of the items the generator will produce, another call to next() raises the StopIteration exception—in this case, with the return value of 3. We can actually see this happen.

01:58 Let’s create our generator.

02:07 Usually, there’s a more complicated process to produce a sequence of values from the yield statements, but here we’re more interested in the effect of the return statement.

02:17 Now we’ll assign the function call to a variable,

02:22 which makes g a generator object. Now we’ll attempt to produce some values from it. next(g) gives us 1.

02:35 The next time we call it, we get the 2. One more time.

02:43 Since there’s nothing left, we get the StopIteration exception, and we can see that its .value is the return value 3.

02:55 So as you saw, we use the generator to produce the values 1 and 2 on demand. We did that by using Python’s built-in function next(), which retrieves the next item from a Python generator.

03:10 The first two calls to next() produce the values 1 and 2 from the yield statements in the function. With a third call to next(), we had used up all of the values the generator was prepared to provide, so a StopIteration exception was raised. Finally, you were able to see the use of a return statement in a generator function.

03:34 In this case, the return value 3 became the value of the exception’s .value attribute. Next, we’ll summarize everything you learned in this course.

Avatar image for cordovez

cordovez on Dec. 13, 2023

I think a slightly more complex example might be needed here to illustrate the usefulness of ‘yield’. Specifically, I am thinking how the library Scrapy uses ‘yield’ when scraping website urls, so that each uri is parsed sequentially, etc.

Become a Member to join the conversation.