Python Tutorial: Decorators
Wrap functions with extra powers. Used by Flask, Django, FastAPI, and every serious Python project.
What you'll learn in this Python decorators tutorial
This interactive Python tutorial has 7 hands-on exercises. Estimated time: 18 minutes.
- Functions are values — Before decorators make sense, you need to know one thing: in Python, functions are values. You can assign them to variab…
- A function that returns a function — A function can also *create and return* a new function. The inner function remembers the outer function's variables — th…
- Your first decorator — A decorator wraps a function with extra behaviour — without changing the original function's code.
- Build a timer decorator ⏱️ — The most useful decorator in the real world: measure how long a function takes.
- Stacking decorators — You can stack multiple decorators. They apply bottom-up: the closest one wraps first, then the next one wraps that.
- The @property decorator — Python's built-in `@property` decorator turns a method into something that looks like an attribute.
- Build a retry decorator 🔄 — Build a `retry(times)` decorator that re-runs a function up to `times` attempts if it raises an exception.