Decorators

wrapping functions with @ — timing, logging, caching

35 min · interactive lesson with runnable code and 6 graded exercises.

What you'll be able to do

  • Explain that @dec means fn = dec(fn) — at definition time
  • Write a timing decorator with functools.wraps
  • Write a decorator that takes arguments (three levels)
  • Predict the order of stacked decorators

The full lesson is free with an account.

The idea

When does it run?

A timing decorator

Decorators with arguments

Stacking decorators

Decorators you already use

Common pitfalls