All Challengesadvanced
Python decorator pattern
Decorators are everywhere in Python - Flask routes, logging, caching. AI generates them constantly.
pythondecoratorshigher-order functionswrapping
python
import time
def timer(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"{func.__name__} took {end - start:.2f}s")
return result
return wrapper
@timer
def slow_sum(numbers):
time.sleep(0.1)
return sum(numbers)
output = slow_sum([1, 2, 3, 4, 5])Question
What does @timer do, and what is the value of output?