Python generator frames: Inspecting locals with gi_frame

Python generator frames: Inspecting locals with gi_frame

When a Python generator yields, its stack frame remains — inspect it for the current line and local variables:

def fib():
    x = 0
    y = 1
    while True:
        yield x
        x, y = y, x+y
g = fib()
next(g)
g.gi_frame.f_locals  # returns {'x': 0, 'y': 1}