← Python Intermediate
14 / 25
Lesson 14 — Decorators
Wrapping functions
A decorator is a function that takes another function and returns a new one that adds behaviour around it — logging, timing, checks. Writing @name on the line above a def is a shortcut for name = deco(name). The original function still runs inside the wrapper.
@timer
def build():
...
# same as: build = timer(build)
Why it matters: frameworks use decorators everywhere, for example to mark a function as a web route.
Want it another way?
Task
Decorate add with @logged so it prints "calling add" before returning the sum.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.