Caffeinated Coder
Book an appointment
Lesson 22 · Idiomatic Python

Decorators

A decorator is a function that takes a function and returns a new one, and @ is the shorthand that applies it. Step through a four-line decorator, shout, as Python hands greet to it, builds a wrapper that remembers the original in a closure, moves the name greet onto the wrapper, and turns 'hi sam' into HI SAM. Finish with functools.wraps, the one line every decorator should have.

14 min10 steps5 questionsBeginner
Fig 22 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.