Python Generators and Iterators: Advanced Test
This advanced Python test covers generators and the iterator protocol, the machinery behind every for loop, comprehension and zip call. Questions check why a generator can be consumed only once, when a generator function's body actually starts running, why a generator expression evaluates its first iterable immediately, how yield from returns the value of a sub-generator's return statement, how send resumes a paused yield and why a generator must be primed with next, why a StopIteration inside a generator becomes a RuntimeError since PEP 479, what happens when the same iterator is passed to zip twice, how zip silently consumes an extra value from a longer iterator, how iter falls back to getitem and why iterators return themselves from iter, and why len does not work on generators. Every question is a complete program you can run in the Python sandbox. The test has 10 multiple-choice questions with correct answers and an explanation for each.
Comentarii