What does the StopIteration exception indicate in generators?
A. Generator reached its end
B. Generator is paused
C. Generator encountered an error
D. Generator is still producing values
Answer: Option A
Solution (By Examveda Team)
The correct answer is Option A: Generator reached its end.The StopIteration exception indicates that a generator has reached its end. In Python, when a generator function exhausts all the values it can yield, it automatically raises a StopIteration exception to signal the end of iteration. This exception is commonly used internally by Python to manage iteration over generator objects. When you iterate over a generator using a for loop or manually using the next() function, Python handles this exception behind the scenes, terminating the iteration gracefully. This mechanism allows for seamless handling of generator exhaustion without the need for explicit termination conditions.
Join The Discussion