{"course":{"slug":"python-for-ai","title":"Python, from Zero, for AI","url":"https://addaly.com/learn/python-for-ai","level":"beginner","track":"build"},"lesson":{"slug":"context-managers","title":"Context managers: the cleanup that runs even when the block raises","url":"https://addaly.com/learn/python-for-ai/context-managers","position":65,"of":89,"minutes":8,"answer":"with guarantees that __exit__ runs however the block ends, which is why files, sessions, locks and timers belong in one; contextlib.contextmanager writes one from a generator with a single yield.","keyIdea":"with guarantees that __exit__ runs however the block ends, which is why files, sessions, locks and timers belong in one; contextlib.contextmanager writes one from a generator with a single yield, and the code after the yield is the cleanup."},"body":{"format":"markdown","text":"## The problem `with` solves\n\n```python\nf = open(path, \"w\")\nfor item in items:\n    f.write(process(item) + \"\\n\")\nf.close()\n```\n\nIf `process` raises on the fortieth item, `f.close()` never runs. What happens to the thirty-nine lines already written depends on buffering: Python collects writes in memory and sends them to disk when the buffer fills or the file is closed. A short run that never closes can leave you with an empty file and a log full of successes. The file is closed when the process exits, but if the interpreter is shutting down because of an unhandled exception, whether that flush happens is not something to rely on.\n\nThe fix you already know:\n\n```python\nwith open(path, \"w\") as f:\n    for item in items:\n        f.write(process(item) + \"\\n\")\n```\n\n`with` promises that the file's cleanup runs whether the block finishes, raises, or is left by `return` or `break`. This lesson is about what that promise is made of and how to make it for your own resources.\n\n## The protocol\n\nA context manager is any object with `__enter__` and `__exit__`. `with` calls `__enter__` first and binds its return value to the name after `as`. When the block ends, by any route, it calls `__exit__` with three arguments describing the exception, or `None, None, None` if there was none.\n\n```python\nclass Timer:\n    def __enter__(self):\n        self.start = time.perf_counter()\n        return self\n    def __exit__(self, exc_type, exc, tb):\n        self.elapsed = time.perf_counter() - self.start\n        log.info(\"block took %.2fs%s\", self.elapsed, \" (failed)\" if exc else \"\")\n        return False\n```\n\nReturning `False` from `__exit__` lets the exception propagate after cleanup. Returning `True` suppresses it, which is appropriate only for a manager whose purpose is to suppress: `contextlib.suppress(FileNotFoundError)` exists for that. Suppressing by accident turns a crash into silent wrong output.\n\n## The generator form\n\nWriting two methods for every small resource is tedious. `contextlib.contextmanager` turns a generator with exactly one `yield` into a context manager:\n\n```python\nfrom contextlib import contextmanager\n\n@contextmanager\ndef timed(label):\n    start = time.perf_counter()\n    try:\n        yield\n    finally:\n        log.info(\"%s: %.2fs\", label, time.perf_counter() - start)\n```\n\n```python\nwith timed(\"embedding 500 chunks\"):\n    vectors = embed(chunks)\n```\n\nEverything before `yield` is `__enter__`; everything after is `__exit__`. The `try/finally` is what makes the cleanup run on an exception: without it, an exception in the block would skip the code after `yield`. Whatever you `yield` becomes the `as` value.\n\nA resource-shaped one:\n\n```python\n@contextmanager\ndef api_session(key):\n    s = requests.Session()\n    s.headers[\"Authorization\"] = f\"Bearer {key}\"\n    try:\n        yield s\n    finally:\n        s.close()\n```\n\n## What belongs in one\n\nAnything that must be undone: an open file, a network session, a database connection or transaction, a lock, a temporary directory, a changed working directory, a changed environment variable, a timer, a progress bar. The test is: \"if the code in the middle blows up, is there something that must still happen?\" If yes, it is a context manager.\n\nThe standard library is full of them. `tempfile.TemporaryDirectory()` creates a folder and deletes it at the end. `threading.Lock()` can be used as `with lock:` so the lock is released even on error, which is the difference between a bug and a deadlock. `sqlite3` connections used with `with conn:` commit on success and roll back on exception: a transaction in one line. `contextlib.redirect_stdout(buffer)` captures prints, useful in tests. `unittest.mock.patch` from module 4 is one, and that is why its effect ends at the close of the block.\n\n## Several at once\n\n```python\nwith open(src) as fin, open(dst, \"w\") as fout:\n    for line in fin:\n        fout.write(transform(line))\n```\n\nComma-separated managers are entered left to right and exited right to left. If the second `open` fails, the first is still closed properly.\n\nWhen the number of resources is not known until runtime (one file per output category, say) `contextlib.ExitStack` collects them:\n\n```python\nfrom contextlib import ExitStack\n\nwith ExitStack() as stack:\n    files = {cat: stack.enter_context(open(f\"{cat}.txt\", \"w\")) for cat in categories}\n    for item in items:\n        files[item.category].write(item.text + \"\\n\")\n```\n\nEvery file is closed when the block ends, in reverse order, however it ends.\n\n## Async\n\n`async with` is the same protocol with `__aenter__` and `__aexit__`, for resources whose setup or teardown awaits: `httpx.AsyncClient()` in module 6 was one. `contextlib.asynccontextmanager` is the generator form. The rules do not change: cleanup after `yield`, `try/finally` around it.\n\n## The mistake to avoid\n\nDo not put the body of the work inside the manager's own code. A context manager sets up, yields, and tears down; the block is the caller's. A `contextmanager` that does a loop of API calls before its `yield` is a function wearing a costume, and its cleanup will not cover the block that matters.\n\n## Try this now\n\nWrite `timed(label)` and wrap a model call in it. Then write a `changed_env(name, value)` manager that sets an environment variable and restores the old value, including when there was none, and prove with a deliberate `raise` inside the block that the variable is restored anyway."},"check":{"question":"A function writes results to a file with `f = open(path, \"w\")`, loops over API calls writing each result, and calls `f.close()` at the end. One call raises midway. When the developer opens the output file afterwards, it is empty, though the log shows twenty successful writes before the failure. Why?","options":[{"text":"Writing to a file inside a loop is disallowed while a network connection is open, so the writes were silently dropped.","correct":false,"tempting":"There is no such restriction; file writes and network calls coexist freely."},{"text":"The exception skipped `f.close()`, so the twenty writes sat in the file's buffer and never reached disk; a `with` block would have closed it on the way out.","correct":true},{"text":"Mode `\"w\"` writes only when the file is closed normally; an exception reverts the file to its original state.","correct":false,"tempting":"There is no rollback in file writing; the writes were buffered rather than reverted."},{"text":"The exception was raised inside `f.write`, so the file was left in a corrupted state and the operating system truncated it to zero bytes as a safety measure when the process exited.","correct":false,"tempting":"The exception came from an API call, and even a failing write does not cause the OS to truncate a file."}]},"prev":{"slug":"the-iterator-protocol","title":"The iterator protocol: what for actually does, and why a generator is empty the second time","url":"https://addaly.com/learn/python-for-ai/the-iterator-protocol"},"next":{"slug":"decorators","title":"Decorators: wrapping a function with retry, timing or a cache","url":"https://addaly.com/learn/python-for-ai/decorators"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Context managers: the cleanup that runs even when the block raises', in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/context-managers (Accessed: 21 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Context managers: the cleanup that runs even when the block raises","courseTitle":"Python, from Zero, for AI","module":{"index":7,"slug":"objects-and-the-shape-of-programs","title":"Objects, generators and the shape a larger program takes"},"answer":null,"keyIdea":"with guarantees that __exit__ runs however the block ends, which is why files, sessions, locks and timers belong in one; contextlib.contextmanager writes one from a generator with a single yield, and the code after the yield is the cleanup.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"the-problem-with-solves","runs":[{"text":"The problem "},{"text":"with","code":true},{"text":" solves"}]},{"t":"code","lang":"python","label":"Python","text":"f = open(path, \"w\")\nfor item in items:\n    f.write(process(item) + \"\\n\")\nf.close()"},{"t":"p","runs":[{"text":"If "},{"text":"process","code":true},{"text":" raises on the fortieth item, "},{"text":"f.close()","code":true},{"text":" never runs. What happens to the thirty-nine lines already written depends on buffering: Python collects writes in memory and sends them to disk when the buffer fills or the file is closed. A short run that never closes can leave you with an empty file and a log full of successes. The file is closed when the process exits, but if the interpreter is shutting down because of an unhandled exception, whether that flush happens is not something to rely on."}]},{"t":"p","runs":[{"text":"The fix you already know:"}]},{"t":"code","lang":"python","label":"Python","text":"with open(path, \"w\") as f:\n    for item in items:\n        f.write(process(item) + \"\\n\")"},{"t":"p","runs":[{"text":"with","code":true},{"text":" promises that the file’s cleanup runs whether the block finishes, raises, or is left by "},{"text":"return","code":true},{"text":" or "},{"text":"break","code":true},{"text":". This lesson is about what that promise is made of and how to make it for your own resources."}]},{"t":"h2","id":"the-protocol","runs":[{"text":"The protocol"}]},{"t":"p","runs":[{"text":"A context manager is any object with "},{"text":"__enter__","code":true},{"text":" and "},{"text":"__exit__","code":true},{"text":". "},{"text":"with","code":true},{"text":" calls "},{"text":"__enter__","code":true},{"text":" first and binds its return value to the name after "},{"text":"as","code":true},{"text":". When the block ends, by any route, it calls "},{"text":"__exit__","code":true},{"text":" with three arguments describing the exception, or "},{"text":"None, None, None","code":true},{"text":" if there was none."}]},{"t":"code","lang":"python","label":"Python","text":"class Timer:\n    def __enter__(self):\n        self.start = time.perf_counter()\n        return self\n    def __exit__(self, exc_type, exc, tb):\n        self.elapsed = time.perf_counter() - self.start\n        log.info(\"block took %.2fs%s\", self.elapsed, \" (failed)\" if exc else \"\")\n        return False"},{"t":"p","runs":[{"text":"Returning "},{"text":"False","code":true},{"text":" from "},{"text":"__exit__","code":true},{"text":" lets the exception propagate after cleanup. Returning "},{"text":"True","code":true},{"text":" suppresses it, which is appropriate only for a manager whose purpose is to suppress: "},{"text":"contextlib.suppress(FileNotFoundError)","code":true},{"text":" exists for that. Suppressing by accident turns a crash into silent wrong output."}]},{"t":"h2","id":"the-generator-form","runs":[{"text":"The generator form"}]},{"t":"p","runs":[{"text":"Writing two methods for every small resource is tedious. "},{"text":"contextlib.contextmanager","code":true},{"text":" turns a generator with exactly one "},{"text":"yield","code":true},{"text":" into a context manager:"}]},{"t":"code","lang":"python","label":"Python","text":"from contextlib import contextmanager\n\n@contextmanager\ndef timed(label):\n    start = time.perf_counter()\n    try:\n        yield\n    finally:\n        log.info(\"%s: %.2fs\", label, time.perf_counter() - start)"},{"t":"code","lang":"python","label":"Python","text":"with timed(\"embedding 500 chunks\"):\n    vectors = embed(chunks)"},{"t":"p","runs":[{"text":"Everything before "},{"text":"yield","code":true},{"text":" is "},{"text":"__enter__","code":true},{"text":"; everything after is "},{"text":"__exit__","code":true},{"text":". The "},{"text":"try/finally","code":true},{"text":" is what makes the cleanup run on an exception: without it, an exception in the block would skip the code after "},{"text":"yield","code":true},{"text":". Whatever you "},{"text":"yield","code":true},{"text":" becomes the "},{"text":"as","code":true},{"text":" value."}]},{"t":"p","runs":[{"text":"A resource-shaped one:"}]},{"t":"code","lang":"python","label":"Python","text":"@contextmanager\ndef api_session(key):\n    s = requests.Session()\n    s.headers[\"Authorization\"] = f\"Bearer {key}\"\n    try:\n        yield s\n    finally:\n        s.close()"},{"t":"h2","id":"what-belongs-in-one","runs":[{"text":"What belongs in one"}]},{"t":"p","runs":[{"text":"Anything that must be undone: an open file, a network session, a database connection or transaction, a lock, a temporary directory, a changed working directory, a changed environment variable, a timer, a progress bar. The test is: “if the code in the middle blows up, is there something that must still happen?” If yes, it is a context manager."}]},{"t":"p","runs":[{"text":"The standard library is full of them. "},{"text":"tempfile.TemporaryDirectory()","code":true},{"text":" creates a folder and deletes it at the end. "},{"text":"threading.Lock()","code":true},{"text":" can be used as "},{"text":"with lock:","code":true},{"text":" so the lock is released even on error, which is the difference between a bug and a deadlock. "},{"text":"sqlite3","code":true},{"text":" connections used with "},{"text":"with conn:","code":true},{"text":" commit on success and roll back on exception: a transaction in one line. "},{"text":"contextlib.redirect_stdout(buffer)","code":true},{"text":" captures prints, useful in tests. "},{"text":"unittest.mock.patch","code":true},{"text":" from module 4 is one, and that is why its effect ends at the close of the block."}]},{"t":"h2","id":"several-at-once","runs":[{"text":"Several at once"}]},{"t":"code","lang":"python","label":"Python","text":"with open(src) as fin, open(dst, \"w\") as fout:\n    for line in fin:\n        fout.write(transform(line))"},{"t":"p","runs":[{"text":"Comma-separated managers are entered left to right and exited right to left. If the second "},{"text":"open","code":true},{"text":" fails, the first is still closed properly."}]},{"t":"p","runs":[{"text":"When the number of resources is not known until runtime (one file per output category, say) "},{"text":"contextlib.ExitStack","code":true},{"text":" collects them:"}]},{"t":"code","lang":"python","label":"Python","text":"from contextlib import ExitStack\n\nwith ExitStack() as stack:\n    files = {cat: stack.enter_context(open(f\"{cat}.txt\", \"w\")) for cat in categories}\n    for item in items:\n        files[item.category].write(item.text + \"\\n\")"},{"t":"p","runs":[{"text":"Every file is closed when the block ends, in reverse order, however it ends."}]},{"t":"h2","id":"async","runs":[{"text":"Async"}]},{"t":"p","runs":[{"text":"async with","code":true},{"text":" is the same protocol with "},{"text":"__aenter__","code":true},{"text":" and "},{"text":"__aexit__","code":true},{"text":", for resources whose setup or teardown awaits: "},{"text":"httpx.AsyncClient()","code":true},{"text":" in module 6 was one. "},{"text":"contextlib.asynccontextmanager","code":true},{"text":" is the generator form. The rules do not change: cleanup after "},{"text":"yield","code":true},{"text":", "},{"text":"try/finally","code":true},{"text":" around it."}]},{"t":"h2","id":"the-mistake-to-avoid","runs":[{"text":"The mistake to avoid"}]},{"t":"p","runs":[{"text":"Do not put the body of the work inside the manager’s own code. A context manager sets up, yields, and tears down; the block is the caller’s. A "},{"text":"contextmanager","code":true},{"text":" that does a loop of API calls before its "},{"text":"yield","code":true},{"text":" is a function wearing a costume, and its cleanup will not cover the block that matters."}]},{"t":"h2","id":"try-this-now","runs":[{"text":"Try this now"}],"role":"try"},{"t":"p","runs":[{"text":"Write "},{"text":"timed(label)","code":true},{"text":" and wrap a model call in it. Then write a "},{"text":"changed_env(name, value)","code":true},{"text":" manager that sets an environment variable and restores the old value, including when there was none, and prove with a deliberate "},{"text":"raise","code":true},{"text":" inside the block that the variable is restored anyway."}]}],"check":{"question":[{"text":"A function writes results to a file with "},{"text":"f = open(path, \"w\")","code":true},{"text":", loops over API calls writing each result, and calls "},{"text":"f.close()","code":true},{"text":" at the end. One call raises midway. When the developer opens the output file afterwards, it is empty, though the log shows twenty successful writes before the failure. Why?"}],"options":[{"text":[{"text":"Writing to a file inside a loop is disallowed while a network connection is open, so the writes were silently dropped."}],"correct":false,"tempting":[{"text":"There is no such restriction; file writes and network calls coexist freely."}]},{"text":[{"text":"The exception skipped "},{"text":"f.close()","code":true},{"text":", so the twenty writes sat in the file’s buffer and never reached disk; a "},{"text":"with","code":true},{"text":" block would have closed it on the way out."}],"correct":true,"tempting":null},{"text":[{"text":"Mode "},{"text":"\"w\"","code":true},{"text":" writes only when the file is closed normally; an exception reverts the file to its original state."}],"correct":false,"tempting":[{"text":"There is no rollback in file writing; the writes were buffered rather than reverted."}]},{"text":[{"text":"The exception was raised inside "},{"text":"f.write","code":true},{"text":", so the file was left in a corrupted state and the operating system truncated it to zero bytes as a safety measure when the process exited."}],"correct":false,"tempting":[{"text":"The exception came from an API call, and even a failing write does not cause the OS to truncate a file."}]}],"order":[2,1,3,0],"why":null},"prev":{"slug":"the-iterator-protocol","title":"The iterator protocol: what for actually does, and why a generator is empty the second time","minutes":9},"next":{"slug":"decorators","title":"Decorators: wrapping a function with retry, timing or a cache","minutes":9}}}