Decorators and Generators

Lesson 16 of 20

Wrap functions with @decorators, pause them with yield — two Python superpowers you will use constantly.

The big picture

Functions that wrap, functions that pause

Two elegant patterns, one idea each: decorators hand your function to another function; generators freeze at yield and resume on demand.

Functions Take Functions

A decorator is a function receiving your function and returning a wrapped upgrade.

@ Is Just Shorthand

@shout above a def means hello = shout(hello) — same thing, nicer to read.

yield = Pause Button

One yield turns a function into a generator: frozen at each yield, resumed by next().

Lazy by Design

Generators produce one value at a time — they never build the whole sequence in memory.