What is the difference between exhausting a generator using "for i in generator" and next(generator)

Question:

I want to learn how to use the return value of a generator (but this not what I’m concerned with now).

After searching, they said that I can get the return value from StopIteration when the generator is exhausted, so I tested it with the following code:

def my_generator():
    yield 1
    yield 2
    yield 3
    return "done"


def exhaust_generator(_gen):
    print("===============================================n")
    print("exhaust_generator")
    try:
        while True:
            print(next(_gen))
    except StopIteration as e:
        print(f"Return value: '{e.value}'")


def exhaust_generator_iter(_gen):
    print("===============================================n")
    print("exhaust_generator_iter")
    try:
        for i in _gen:
            print(i)
        print(next(_gen))
    except StopIteration as e:
        print(f"Return value: {e.value}")


gen = my_generator()
gen2 = my_generator()
exhaust_generator(gen)
exhaust_generator_iter(gen2)

===============================================

exhaust_generator
1
2
3
Return value: 'done'
===============================================

exhaust_generator_iter
1
2
3
Return value: None

As you can see, the return value is different between the two versions of exhausting the generator and I wonder why.

Searched google but it has not been helpful.

Asked By: Chicky

||

Answers:

In your first example with the while, you’re catching the first StopIteration exception raised by the generator when it’s initially exhausted.

In your second example with the for, you’re catching a subsequent StopIteration exception raised by calling next() a second time after the generator has already been exhausted (by the for, which caught that initial exception).

Answered By: Samwise

As far as I understand, the for loop internally also calls next() and eventually catches the StopIteration, so it knows when to stop calling next().

Your next() call in exhaust_generator_iter then raises a second StopIteration but without the passed value.

This SO question and its answers explore ways to receive the return value from a generator, i.a. using a utility class or dependency injection.

Answered By: buddemat
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.