Introduction
You’ve used with open("file.txt") as f: a hundred times. But why does it work? And how can you write your own? Context managers are Python’s way of guaranteeing cleanup — files closed, locks released, DB transactions committed or rolled back — even when errors occur.
The problem they solve
# Without context manager: leak risk
f = open("data.txt")
data = f.read()
# If an exception happens here, f is never closed!
f.close()
# With context manager: guaranteed cleanup
with open("data.txt") as f:
data = f.read()
# f is closed here, even if an exception occurred
The with statement ensures __exit__ runs no matter what — normal completion, exception, or early return.
How with works (the protocol)
Any object with __enter__ and __exit__ methods can be used with with:
class ManagedFile:
def __init__(self, path):
self.path = path
self.f = None
def __enter__(self):
self.f = open(self.path)
return self.f # bound to `as` variable
def __exit__(self, exc_type, exc_val, exc_tb):
if self.f:
self.f.close()
# Return True to suppress the exception, False/None to propagate
return False
with ManagedFile("data.txt") as f:
data = f.read()
# f is closed here
__exit__ receives exception info (exc_type, exc_val, exc_tb) if an exception occurred, so you can decide whether to handle it or let it propagate.
contextlib — the easy way
Writing classes is verbose. Use @contextmanager from contextlib:
from contextlib import contextmanager
@contextmanager
def managed_file(path):
f = open(path)
try:
yield f # __enter__ returns this
finally:
f.close() # __exit__ runs this
with managed_file("data.txt") as f:
data = f.read()
yield separates setup (before) from teardown (after). The try/finally ensures cleanup even on exceptions.
Real-world examples
1. Database transaction
from contextlib import contextmanager
@contextmanager
def transaction(conn):
try:
yield conn
conn.commit()
except Exception:
conn.rollback()
raise
with transaction(conn) as c:
c.execute("INSERT INTO users (name) VALUES (?)", ("Galvan",))
# commits on success, rolls back on error
2. Timer
import time
from contextlib import contextmanager
@contextmanager
def timer(label):
start = time.perf_counter()
yield
elapsed = time.perf_counter() - start
print(f"{label}: {elapsed:.4f}s")
with timer("data processing"):
process_data() # prints "data processing: 1.2345s"
3. Temporary directory
import tempfile, os
from contextlib import contextmanager
@contextmanager
def temp_dir():
d = tempfile.mkdtemp()
try:
yield d
finally:
import shutil
shutil.rmtree(d)
with temp_dir() as d:
# use d for temp files
with open(os.path.join(d, "test.txt"), "w") as f:
f.write("hello")
# directory cleaned up
4. Lock (threading)
import threading
lock = threading.Lock()
with lock: # lock.__enter__ acquires, __exit__ releases
# critical section
shared_data.append(item)
5. Suppress exceptions
from contextlib import suppress
with suppress(FileNotFoundError):
os.remove("maybe_exists.txt")
# No error if file doesn't exist
ExitStack — dynamic context managers
For a variable number of context managers:
from contextlib import ExitStack
files = ["a.txt", "b.txt", "c.txt"]
with ExitStack() as stack:
handles = [stack.enter_context(open(f)) for f in files]
# all files open; all closed on exit
for f in handles:
print(f.read())
Async context managers
For async code, use async with:
import aiohttp
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
return await resp.text()
Define with @asynccontextmanager:
from contextlib import asynccontextmanager
@asynccontextmanager
async def managed_conn():
conn = await create_connection()
try:
yield conn
finally:
await conn.close()
Common mistakes
- Not using
try/finally— if you yield without finally, exceptions skip cleanup. - Returning True from
__exit__— suppresses exceptions silently. Only do this intentionally. - Forgetting
yield—@contextmanagerfunctions must yield exactly once. - Using
withfor non-context-managers — objects need__enter__/__exit__. - Nesting too deeply — use
ExitStackfor many context managers.
Key Concepts
- Context manager = object with
__enter__and__exit__ with= calls__enter__, binds result toas, calls__exit__on exit@contextmanager= decorator that converts a generator into a context manager__exit__return value = True suppresses exception, False/None propagatesExitStack= manage a dynamic number of context managersasync with= async version for async code
What to Try Next
- Wrap your DB connections in context managers.
- Add a timer context manager to your Streamlit app for profiling.
- Use
suppressfor optional file operations. - Combine with decorators for cross-cutting concerns.
FAQ
Context manager vs try/finally?
Context managers are try/finally packaged as reusable objects. Use try/finally for one-off cleanup; context managers for reusable patterns.
Can I use multiple context managers?
Yes — comma-separated: with open("a") as f, open("b") as g:. Or nested. Or ExitStack for dynamic counts.
contextlib.suppress vs try/except: pass?
suppress is cleaner and more explicit. Use it for ignoring specific exceptions. For complex error handling, use try/except.