Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Yield having different behavior when iterated

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading