On this page
article
Custom Context Manager
Manage resources with contextlib.
Category: contextlib
Problem
Manage resources with contextlib.
Solution
@contextmanager
def timer():
start = time.time()
yield
print(time.time() - start)
Notes
- Adapt variable names and paths to your project
- Add error handling for production use
- See related chapters in the Learning Path