Q23: How do Generators and the 'yield' statement operate?
Functions containing yield return a Generator Iterator. When next() is called, execution proceeds until reaching yield, returning the value and suspending execution frame state in memory. Subsequent next() calls resume execution immediately following yield.
Key Takeaways
- Suspends function execution state without destroying frame memory.
- Provides lazy evaluation and stream processing with $O(1)$ memory usage.
Knowledge Check
What exception is raised when a generator exhausts all yielded values during iteration?