Back to library

🐍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.

Applied14 drops~2-week path · 5–8 min/daytechnology

Phase 1Functions as Values and Why Decorators Exist

See functions as values and why decorator syntax exists

4 drops
  1. Every function in Python is an object

    5 min

    Every function in Python is an object

  2. Functions that take and return functions

    6 min

    Functions that take and return functions

  3. @decorator is one line of rebinding

    6 min

    @decorator is one line of rebinding

  4. Wrappers remember the function they wrap

    6 min

    Wrappers remember the function they wrap

Phase 2Writing Decorators With and Without Arguments

Write decorators with args, defaults, and varargs safely

5 drops
  1. Accept anything with *args and **kwargs

    6 min

    Accept anything with *args and **kwargs

  2. Always return what the wrapped function returned

    6 min

    Always return what the wrapped function returned

  3. A decorator factory is a function that returns a decorator

    7 min

    A decorator factory is a function that returns a decorator

  4. Decorators with sensible defaults

    6 min

    Decorators with sensible defaults

  5. Store per-function state in the closure

    7 min

    Store per-function state in the closure

Phase 3functools.wraps, Classes, and Stacking

Connect functools.wraps, class decorators, and stacking order

4 drops
  1. Every decorator needs @wraps

    6 min

    Every decorator needs @wraps

  2. Decorators that return a callable class

    7 min

    Decorators that return a callable class

  3. Stacked decorators apply bottom-up

    7 min

    Stacked decorators apply bottom-up

  4. When to reach for a decorator and when not to

    7 min

    When 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

1 drop
  1. Build a cache decorator from scratch

    20 min

    Build 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.