CoursePython from Scratch to Intermediate · Module 10: Intermediate Python · part 68 of 75
Part 68 · Module 10: Intermediate Python

Lesson 2: Decorators: wrapping a function without changing it

16 min read·14 Oct 2026
Module 10 · Lesson 2 · About 45 minutes · Needs: Module 6 Lesson 3 (functions as values, nested functions, nonlocal), Module 6 Lesson 2 (*args, **kwargs), Module 7 (the booknook package)

The situation

Amira's request for this module is "Make sure it never breaks." Before anything can be tested, she wants to see what the shelf's code is doing. When a fee looks wrong, she asks: which function was called, with what, and what did it answer?

A print inside every rule would clutter it. This lesson adds the note from outside, with a decorator: a function that wraps another function and adds work around it. You will follow Ben's L13, 9 days late, which must still cost 180 pence.

late_fee(9) Ben's L13 log_calls wrapper real late_fee 180 same answer called late_fee(9) -> 180
A decorator wraps the fee rule (blue, your code around it): Ben's call still answers 180 pence, and a log line saying what happened is printed on the way.

The rest of this part opens with an account

Signing up is free and takes one email. Nothing to pay, and nothing renews.