🐍Python Decorators Introduction
Build one mental model for Python decorators that covers closures, argument passing, functools.wraps, and stacking — then ship a working caching or logging decorator from scratch in under 30 lines.
Phase 1Functions as Values and Why Decorators Exist
See functions as values and why decorator syntax exists
Every function in Python is an object
5 minEvery function in Python is an object
Functions that take and return functions
6 minFunctions that take and return functions
@decorator is one line of rebinding
6 min@decorator is one line of rebinding
Wrappers remember the function they wrap
6 minWrappers remember the function they wrap
Phase 2Writing Decorators With and Without Arguments
Write decorators with args, defaults, and varargs safely
Accept anything with *args and **kwargs
6 minAccept anything with *args and **kwargs
Always return what the wrapped function returned
6 minAlways return what the wrapped function returned
A decorator factory is a function that returns a decorator
7 minA decorator factory is a function that returns a decorator
Decorators with sensible defaults
6 minDecorators with sensible defaults
Store per-function state in the closure
7 minStore per-function state in the closure
Phase 3functools.wraps, Classes, and Stacking
Connect functools.wraps, class decorators, and stacking order
Every decorator needs @wraps
6 minEvery decorator needs @wraps
Decorators that return a callable class
7 minDecorators that return a callable class
Stacked decorators apply bottom-up
7 minStacked decorators apply bottom-up
When to reach for a decorator and when not to
7 minWhen to reach for a decorator and when not to
Phase 4Build a Caching or Logging Decorator
Ship a small caching or logging decorator from scratch
Build a cache decorator from scratch
20 minBuild a cache decorator from scratch
Frequently asked questions
- What exactly is a Python decorator and why does @ exist?
- This is covered in the “Python Decorators Introduction” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- Why do I need functools.wraps on every decorator?
- This is covered in the “Python Decorators Introduction” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- How does a decorator that takes arguments actually work?
- This is covered in the “Python Decorators Introduction” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- When should I use a class decorator instead of a function decorator?
- This is covered in the “Python Decorators Introduction” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- In what order do stacked decorators run?
- This is covered in the “Python Decorators Introduction” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Related paths
🦀Rust Lifetimes Explained
Stop reading `'a` as line noise and start reading it as scope arithmetic — one failing snippet at a time — until you can thread lifetimes through a small parser or iterator adapter without fighting the borrow checker.
☸️Kubernetes Core Concepts
Stop drowning in 30+ resource types. Build the mental model one primitive at a time -- pods, deployments, services, ingress, config -- then deploy a real app with rolling updates and health checks.
💻Elixir Pattern Matching
Stop reading `=` as assignment and start using it as Elixir's core flow-control tool — through function heads, guards, and `with` — until you can rewrite a tiny command parser without a single `if`.
📈Big O Intuition
Stop treating Big O as math you memorized for an interview — build the intuition to spot O(n²) disasters, pick the right data structure without thinking, and rewrite a slow function from O(n²) to O(n) in under five minutes.