MemotivaContext managers and the with statement

How do you create a context manager using a class?

Context managers and the with statement

Audio flashcard · 0:13

Nortren·

How do you create a context manager using a class?

0:13

Define a class with dunder enter and dunder exit methods. The dunder enter method is called when entering the with block and its return value is bound to the as variable. The dunder exit method is called when leaving the block, receiving exception info if one occurred.
docs.python.org