Skip to contentSkip to navigation

Protocols and Advanced Iteration

Protocols & Iteration — cheat sheet

Memory-efficient streaming with generators, context managers, and type hints.

Generators & yield

def gen():
    yield 1
    yield 2
Lazy evaluation pausing at yield.
item = next(my_gen)
Advance generator to next yielded value.

Context managers & types

with open(path) as f: pass
Guaranteed cleanup on block exit.
def fn(x: int) -> str: pass
Type hints for parameters and return.
All cheat sheets