MemotivaContext managers and the with statement

What is a context manager?

Context managers and the with statement

Audio flashcard · 0:17

Nortren·

What is a context manager?

0:17

A context manager is an object that defines runtime context for a block of code, with setup and cleanup actions. It is used with the with statement. Context managers implement dunder enter and dunder exit. The most common example is opening a file: with open as f ensures the file is closed.
docs.python.org