Lesson 30 of 55
PythonDecorators
20 minutes
Course progress55%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 30 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
A decorator is a higher‑order function that wraps another function to extend its behavior without modifying the original code.
def debug(func):
def wrapper(*args, **kwargs):
print(f'Calling {func.__name__} with args={args}, kwargs={kwargs}')
result = func(*args, **kwargs)
print(f'{func.__name__} returned {result}')
return result
return wrapper
@debug
def add(a, b):
return a + b
add(3, 4)
The @debug line is syntactic sugar for add = debug(add).