fork download
  1. def a_generator_func():
  2. print("about to yield first value")
  3. yield "first value"
  4. print("just yielded first value")
  5.  
  6. print("about to yield second value")
  7. yield "second_value"
  8. print("done yielding, there is no third value")
  9.  
  10.  
  11. a_generator = a_generator_func()
  12.  
  13. print("About to get first value")
  14. print("The first value is", next(a_generator))
  15. print("About to get second value")
  16. print("The second value is", next(a_generator))
  17. try:
  18. print("About to get third value")
  19. print("The third value is", next(a_generator))
  20. except StopIteration:
  21. print("The generator ended")
  22.  
  23. # for elem in a_generator_func():
  24. # print(elem)
Success #stdin #stdout 0.02s 9220KB
stdin
Standard input is empty
stdout
About to get first value
about to yield first value
The first value is first value
About to get second value
just yielded first value
about to yield second value
The second value is second_value
About to get third value
done yielding, there is no third value
The generator ended