Build reusable resource management with Python context managers
Quick Answer
Use `@contextlib.contextmanager` with a `yield` for simple cases, or implement `__enter__`/`__exit__` on a class for complex cases — both enable the `with` statement for guaranteed cleanup.
1from contextlib import contextmanager
2import time
3
4# 1. Generator-based context manager (simplest approach)
5@contextmanager
6def timer(label: str = 'Block'):
7 start = time.perf_counter()
8 try:
9 yield
10 finally:
11 elapsed = time.perf_counter() - start
12 print(f'{label} took {elapsed:.4f}s')
13
14with timer('Data processing'):
15 time.sleep(0.1) # your code here
16
17# 2. Class-based context manager
18class DatabaseConnection:
19 def __init__(self, url: str):
20 self.url = url
21 self.conn = None
22
23 def __enter__(self):
24 print(f'Connecting to {self.url}')
25 self.conn = object() # replace with real connection
26 return self.conn
27
28 def __exit__(self, exc_type, exc_val, exc_tb):
29 print('Closing connection')
30 self.conn = None
31 return False # re-raise exceptions
32
33with DatabaseConnection('postgresql://localhost/mydb') as conn:
34 pass # use conn here
35
36# 3. Async context manager
37from contextlib import asynccontextmanager
38
39@asynccontextmanager
40async def async_timer(label: str):
41 start = time.perf_counter()
42 try:
43 yield
44 finally:
45 print(f'{label}: {time.perf_counter() - start:.4f}s')Context managers let you manage resources — database connections, file handles, locks, timers — in a clean, exception-safe way using the with statement. You can create your own using either the class-based approach (__enter__ and __exit__) or the simpler generator-based approach with contextlib.contextmanager.
Use `@contextmanager` for simple cases where the generator syntax is cleaner. Use the class-based approach when you need to store state across `__enter__` and `__exit__`, inherit from the context manager, or reuse it multiple times.
This free python code snippet for custom context manager is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this advanced-level example will help you implement custom context manager quickly and correctly.
All snippets in the Snippetly library follow python best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses python.
Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.