Code Reference

Context Managers

Python · Reference cheat sheet

Context Managers

Python · Reference cheat sheet


📋 Overview

Context managers guarantee setup/teardown via with. They close files, release locks, and restore state even when exceptions occur. Implement with __enter__/__exit__ or @contextlib.contextmanager.

🔧 Core concepts

APIRole
with cm as x:Enter, bind result, exit on leave
Multiplewith a, b:
ParenthesizedMulti-line with (3.10+)
__enter__Setup; return bound value
__exit__Cleanup; return truthy to suppress exc
contextlib.contextmanagerGenerator-based CM
ExitStackDynamic stack of contexts
AsyncExitStack / async withAsync variants

__exit__(exc_type, exc, tb) — return True to suppress the exception (rare).

💡 Examples

Built-in file and lock:

from pathlib import Path
from threading import Lock

lock = Lock()
path = Path("out.txt")

with lock, path.open("w", encoding="utf-8") as f:
    f.write("hello\n")

Generator context manager:

from contextlib import contextmanager
from collections.abc import Iterator

@contextmanager
def temp_attr(obj: object, name: str, value: object) -> Iterator[None]:
    old = getattr(obj, name)
    setattr(obj, name, value)
    try:
        yield
    finally:
        setattr(obj, name, old)

class C:
    mode = "prod"

with temp_attr(C, "mode", "test"):
    assert C.mode == "test"

Class-based:

class suppress_os:
    def __enter__(self) -> "suppress_os":
        return self

    def __exit__(self, exc_type, exc, tb) -> bool:
        return exc_type is not None and issubclass(exc_type, OSError)

ExitStack:

from contextlib import ExitStack
from pathlib import Path

paths = [Path("a.txt"), Path("b.txt")]
with ExitStack() as stack:
    files = [stack.enter_context(p.open("w", encoding="utf-8")) for p in paths]
    for f in files:
        f.write("x\n")

⚠️ Pitfalls

  • Returning True from __exit__ swallows exceptions — usually wrong.
  • Yielding inside @contextmanager after errors needs careful try/finally.
  • Nested with order: enter left-to-right, exit right-to-left.
  • Don't open resources without with unless you manage close yourself.
  • Async code needs async with and async context managers.

On this page