Context Manager
Context Manager — A context manager handles resource setup and teardown using the `with` statement, ensuring resources are properly released. Context managers are most commonly used for file operations, database connections, and locks. The `with` statement calls `__enter__` on setup and `__exit__` on teardown (even if an exception occurs). The `contextlib` module provides utilities like `@contextmanager` decorator and `contextlib.suppress()`. This term relates to the Built-in Functions category of the reference. You will meet context manager in practice when working with APIs such as print(), len(), range(), type(). Related terms: Decorator, Generator, GIL (Global Interpreter Lock), Duck Typing, List Comprehension, Mutable vs Immutable. Understanding context manager makes the rest of the Python documentation noticeably easier to follow, because the concept recurs across function references, guides, snippets, and error messages you will meet in real projects. Look up more definitions in the Python glossary, or put the concept into practice with an example from the Python playground.