Python Snippet
Custom Context Manager (Class-Based)
Difficulty: Medium
A class-based context manager defines `__enter__` and `__exit__` so the object can be used in a `with` block. It is the right shape when the resource has setup, teardown, AND state you want to expose to the body (file handles, DB connections, locks). This entry shows the basic skeleton, an exception-aware variant, and the gotchas around return values from `__exit__`.
