Skip to main content
grasp.study
Back to course
Log in
Get started
Someone wanted to learn this too, so Grasp built them a personal learning path.
Create your own
Python Context Managers and ContextVars
·
Module 1
Seeing What a Generator Is and How `yield` Pauses It
1
Comparing Normal Functions and Generator Functions
Call a normal function and a function containing `yield`, then use prints to compare when their bodies run and what each call returns.
2
Verifying Generator and Iterator Objects in Python
Use `type()`, `inspect.isgenerator()`, and `iter(g) is g` to verify that an object is both a generator and an iterator.
3
Understanding Generator Objects: Pausing and Resuming Function Execution
Describe a generator object as one particular function execution that can start, pause, remember its state, and continue.
4
Starting and Pausing a Generator with `next()`
Call `next()` once and observe that the generator starts and pauses at its first `yield`.
5
Resuming Execution After `yield`
Call `next()` again and observe that execution continues immediately after the previous `yield`.
6
Verifying Local Variable Persistence Across Generator Yields
Print changing local variables before each `yield` to verify that their values survive while the generator is paused.
7
Inspecting a Generator’s Saved Locals and Paused Instruction Position
Inspect `g.gi_frame.f_locals` and `g.gi_frame.f_lasti` to find the saved local values and paused instruction position of a generator.
8
Distinguishing Paused and Permanently Finished Generators
Catch `StopIteration` and use prints to distinguish a paused generator from one that has finished permanently.
Next module isn't ready yet,
Back to course
Previous module
Next module