Back to Course
Python Essentials: The Engineering Approach
Module 18 of 20
18. Metaprogramming & Decorators
1. Decorators
Functions that modify other functions.
pythondef log(func): def wrapper(*args, **kwargs): print(f"Calling {func.__name__}") return func(*args, **kwargs) return wrapper @log def add(a, b): return a + b
2. Context Managers
The with statement.
pythonclass Timer: def __enter__(self): self.start = time.time() def __exit__(self, *args): print(f"Time: {time.time() - self.start}") with Timer(): heavy_computation()