← All Exercises

Python Decorator Pattern

intermediatepython~20min

Background

Decorators modify function behavior without changing code, used for logging, timing, caching, and access control.

Problem

Write a timing decorator that wraps a function, measures its execution time, prints it, and returns the original result.

Input

A function to decorate.

Output

A wrapped function that logs execution time and returns the original result.

Sample tests

Sample 1

Input: slow_function() with timing decorator
Expected: prints "Executed in X.XXs" and returns result

Your solution

Learning objective

Learn decorator implementation, functools.wraps, and function metadata preservation.