| id |
modern/context_manager |
| name |
Context Manager |
| aliases |
with-statement |
RAII |
resource-management |
|
| guide_url |
|
| problem |
Guarantee acquire/release pairing around a block of code, even when it raises. |
| symptoms |
forgot to close |
cleanup on exception |
try/finally everywhere |
temporary state that must be restored |
|
| verdict |
pythonic |
| caveats |
@contextlib.contextmanager wants the yield inside try/finally — without it, an exception in the body skips your cleanup. |
Returning True from __exit__ swallows the exception; do it only on purpose. |
|
| stdlib_sightings |
open |
contextlib.contextmanager |
contextlib.ExitStack |
tempfile.TemporaryDirectory |
|
Pair acquire with release on every exit path, structurally — Python's RAII.
Verdict: pythonic — any acquire/release pair you write twice deserves one.
uv run python -m patterns.modern.context_manager.examples.atomic_deploy.main