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 2Lazy evaluation pausing at yield.
item = next(my_gen)Advance generator to next yielded value.
Context managers & types
with open(path) as f: passGuaranteed cleanup on block exit.
def fn(x: int) -> str: passType hints for parameters and return.