Python Context Managers and the with Statement
SkillVeris Team
Engineering Team

A context manager guarantees setup and cleanup around a block of code, so resources are always released even when errors occur.
In this guide, you'll learn:
- The with statement is the syntax that runs a context manager, calling its enter logic on entry and exit logic on the way out.
- The classic example is with open(file) as f, which closes the file automatically no matter what happens inside.
- You can write a class-based context manager by defining __enter__ and __exit__ methods.
- The contextlib.contextmanager decorator lets you build one from a simple generator function with a single yield.
1What Is a Context Manager?
A context manager is an object that defines what should happen when you enter and leave a block of code, guaranteeing that cleanup runs even if an error is raised inside. The with statement is how you use one. Together they ensure resources like files, network connections, and locks are always released.
The most familiar case is opening a file. Writing with open('data.txt') as f automatically closes the file when the block ends, whether it finishes normally or an exception interrupts it. Without this, a forgotten close can leak file handles and corrupt data.
2Why the with Statement Matters
Manual cleanup is easy to get wrong. If you open a file and an exception fires before your close call, the file stays open. The with statement removes that risk by tying cleanup to the block itself, so it always runs. Compare the fragile manual pattern with the safe one.
- f = open('data.txt') # manual: risky
- data = f.read() # if this raises, close never runs
- f.close()
- with open('data.txt') as f: # safe: always closes
- data = f.read()
🔑Key Takeaway
The with statement guarantees cleanup. Even if the code inside raises an exception, the context manager's exit logic still runs before the error propagates.
3How They Work Under the Hood
A context manager is any object with two methods: __enter__ and __exit__. When execution reaches a with block, Python calls __enter__ and binds its return value to the name after as. When the block ends for any reason, Python calls __exit__, passing details of any exception that occurred so the manager can respond or clean up.
- __enter__(self): run setup, return the resource.
- __exit__(self, exc_type, exc_value, traceback): run cleanup.
- If __exit__ returns True, it suppresses the exception.
- Returning False or None lets the exception propagate normally.
4Writing a Class-Based Context Manager
To build your own, define a class with __enter__ and __exit__. This example times how long a block takes, printing the duration whether the block succeeds or fails.
A Timer Example
__enter__ records the start time and returns the manager; __exit__ computes and reports the elapsed time. Because __exit__ always runs, the timing is reliable.
import time
class Timer:
def __enter__(self):
self.start = time.perf_counter()
return self
def __exit__(self, exc_type, exc_value, tb):
elapsed = time.perf_counter() - self.start
print(f'Took {elapsed:.3f}s')
with Timer():
do_work()5The Simpler Way: contextlib
Writing a full class is often overkill. The contextlib.contextmanager decorator turns a generator into a context manager: everything before the single yield is setup, and everything after it is cleanup. The yielded value becomes the as target.
- from contextlib import contextmanager
- @contextmanager
- def open_db():
- conn = connect() # setup
- try:
- yield conn # hand the resource to the block
- finally:
- conn.close() # cleanup, always runs
💡Pro Tip
Put your cleanup in a finally block inside the generator. That ensures the resource closes even if the code using it raises an exception.
6Common Real-World Uses
Context managers show up all over the standard library and popular frameworks because so many resources need reliable cleanup.
- Files: with open(...) closes the handle.
- Locks: with threading.Lock() releases the lock automatically.
- Database sessions: connections and transactions commit or roll back on exit.
- Temporary changes: temporarily change a setting and restore it afterward.
- Suppressing errors: contextlib.suppress ignores specific exceptions cleanly.
7Common Mistakes to Avoid
A few misunderstandings keep beginners from using context managers effectively.
- Still calling close() manually inside a with block; the manager already handles it.
- Forgetting the try/finally in a generator-based manager, so cleanup is skipped on errors.
- Accidentally suppressing exceptions by returning True from __exit__ when you did not mean to.
- Opening resources without with and relying on garbage collection to clean up eventually.
- Nesting many with statements when you can combine them: with a() as x, b() as y.
8Key Takeaways
Context managers make resource handling safe and readable.
- A context manager guarantees cleanup runs, even on exceptions.
- The with statement calls __enter__ on entry and __exit__ on exit.
- Write class-based managers with those two methods.
- Use @contextmanager with a generator for the simpler cases.
- They keep files, locks, and connections from leaking.
9Frequently Asked Questions
Q: What does the with statement do in Python? A: It runs a context manager, which executes setup code on entry and guaranteed cleanup code on exit. The most common use is with open(...) as f, which closes the file automatically when the block ends.
Q: How do I write my own context manager? A: Either define a class with __enter__ and __exit__ methods, or decorate a generator function with contextlib.contextmanager, placing setup before a single yield and cleanup after it.
Q: Does cleanup still run if my code raises an exception? A: Yes. That is the whole point. The __exit__ method (or the finally block in a generator-based manager) runs before the exception propagates, so resources are released reliably.
Q: Can I use multiple context managers at once? A: Yes. Separate them with commas in a single statement, like with open('a') as a, open('b') as b:. Both are entered and exited correctly.
Related Reading
Get The Print Version
Download a PDF of this article for offline reading.
About the Publisher
SkillVeris Team
Engineering Team
Our engineering writers turn abstract code concepts into hands-on, project-driven learning experiences.
View all postsRelated Posts
Never miss an update
Get the latest tutorials and guides delivered to your inbox.
No spam. Unsubscribe anytime.