Yield having different behavior when iterated

Question:

I have the following function::

def f123():
  lista = range(2)
  print("2: before yields")

  yield [lista, "A: yield"]
  print("- after yield A")

  yield [lista, "B: yield"]
  print("- after yield B")

  yield [lista, "C: yield"]
  print("- after yield C")

From what I’ve researched, I can take advantage of my generator by iterating over it or using the next() function to move the cursor.
So, I tried to implement two different ways in order to get the same answer, but without success!

Example 1:

print("0: -----")
print("1: start")

list_gener = f123()

example = list()
example.append(next(list_gener))
example.append(next(list_gener))
example.append(next(list_gener))

print("3: end")
print(example)

Example 2:

print("0: -----")
print("1: start")

example = [item for item in f123()]

print("3: end")
print(example)

Contents of the example variable:

[
   [range(0, 2), 'A: yield'], 
   [range(0, 2), 'B: yield'], 
   [range(0, 2), 'C: yield']
]

Respective Answers:

Answer example 1 Answer example 2
0: —– 0: —–
1: start 1: start
2: before yields 2: before yields
after yield A after yield A
after yield B after yield B
3: end after yield C
3: end

MY DOUBT IS:
What causes after yield C to be printed during loop iteration?

I know that in the next example (example 1) it will never be printed because there is no more yield after that C, but how do I make example 1 behave the same as example 2?

Asked By: José Victor

||

Answers:

You don’t get After yield C in example 1 because you never call next(list_gener) after getting C: yield. The generator is still suspended after the third yield call, waiting for you to call next() again to continue.

In example 2, iterating over the generator keeps calling next() until it gets a StopIteration exception. That happens when it calls again after getting C: yield, so the generator function continues to that print() statement.

You can get equivalent results if you call next() in a loop until the exception.

try:
    while True:
        example.append(next(list_gener))
except StopIteration:
    print("StopIteration")
Answered By: Barmar

a generator doesn’t return, it instead throws a StopIteration Exception once it reaches the end of its code to signal that it is done.

in your example2 when you use for it is going to call next repeatedly until it catches a StopIteration Exception.

in your example1 you only call next 3 times, so it reaches the third yield, but it never ran the code after it because next wasn’t called again.

calling next another time in example 1 will cause the function to run to the end (therefore printing "- after yield C") then it is going to raise a StopIteration Exception which you should catch.

print("0: -----")
print("1: start")

list_gener = f123()

example = list()
example.append(next(list_gener))
example.append(next(list_gener))
example.append(next(list_gener))
try:
    example.append(next(list_gener))
except StopIteration:
    print("caught the exception")

print("3: end")
print(example)
Answered By: Ahmed AEK

In your second example the comprehension loops until it catches a StopIteration exception from the generator, which happens automatically when the generator returns. When you manually step through the generator using next you aren’t calling it a final time to get that StopIteration exception that indicates the generator is "empty".

Change your first test to something like this instead:

# ...
example = list()
try:
    while True:
        example.append(next(list_gener))
except StopIteration:
    pass
# ...
Answered By: Woodford
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.