Two elegant patterns, one idea each: decorators hand your function to another function; generators freeze at yield and resume on demand.
1 Functions Take Functions
A decorator is a function receiving your function and returning a wrapped upgrade.
2 @ Is Just Shorthand
@shout above a def means hello = shout(hello) — same thing, nicer to read.
3 yield = Pause Button
One yield turns a function into a generator: frozen at each yield, resumed by next().
4 Lazy by Design
Generators produce one value at a time — they never build the whole sequence in memory.
Functions that wrap, functions that pause
Functions Take Functions1A decorator is a function receiving your function and returning a wrapped upgrade. @ Is Just Shorthand2@shout above a def means hello = shout(hello) — same thing, nicer to read. yield = Pause Button3One yield turns a function into a generator: frozen at each yield, resumed by next(). Lazy by Design4Generators produce one value at a time — they never build the whole sequence in memory. 4 building blocks make up Functions that wrap, functions that pause
Press Run to see the decorator machinery assemble a better hello.
Tab one shows the decorator rewiring hello; tab two drives a generator three times with next().
mainshout is a function that takes a function — that is all a decorator is.
Memory
no variables yet — press play
Console
output appears as the program runs…
Frozen, woken by next(), running to the next yield, freezing again — until the yields run out.
process decision input / output start / end A generator that climbs by twos — trace the pauses.
Your predicted output — one line per console line
Hint: Each next() resumes right after the previous yield and runs to the next one. This generator steps by 2.
01@decorator is sugar for func = decorator(func).
02The wrapper usually calls the original, adds something, and returns the result.
03One yield turns a function into a generator; next() drives it forward.
04Each next() resumes right after the last yield and runs to the next one.
05When there is nothing left to yield, the generator raises StopIteration and ends.