15
Lesson 32 of 44

Context managers

Learn to manage resources with the with statement. In this lesson you will see what a context manager is and how a resource is closed even when there are errors, create your own managers with classes (__enter__ and __exit__) and with the contextmanager decorator, use the contextlib utilities (suppress, redirect_stdout, ExitStack) and apply good practices in exercises such as transactions with rollback, temporary settings and logs that react to errors.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

What is a context manager: the with statement

Many operations have a beginning and an end that must be respected: opening and closing a file, locking and releasing a resource…

Theory
Read more

Creating context managers with classes

For your own class to work with with you only need to define __enter__ and __exit__ . Let us see how they behave and what they…

Theory
Read more

Managers with @contextmanager

Writing a class with two methods is a lot of code for something simple. The contextlib module offers the @contextmanager…

Theory
Read more

contextlib utilities

Besides @contextmanager , the contextlib module includes ready-made managers for very common situations. Knowing them avoids…

Theory
Read more

Best practices with with

You already know how to create and use context managers. This last article gathers the habits that avoid mistakes and several…

Theory
Read more

Practice exercises 10

Section with entry and exit

Create a Python program with a class that is a context manager : an object that can be used with with thanks to the __enter__ and…

Exercise Easy
Solve it

Nested levels

Create a Python program that nests several with blocks one inside another and shows each opening and closing with an indentation…

Exercise Medium
Solve it

Capture the output

Create a Python program that captures what is printed with print inside a with block, using an io.StringIO object from the io…

Exercise Medium
Solve it

Decorative frame

Create a Python program with a context manager written as a generator function and the @contextmanager decorator from the…

Exercise Medium
Solve it

Ignore errors

Create a Python program that adds up numbers while ignoring texts that are not numbers, using suppress from the contextlib module…

Exercise Medium
Solve it

Guaranteed cleanup

Create a Python program that shows that a context manager is always closed , even when an error occurs inside the block, and that…

Exercise Medium
Solve it

Transaction with rollback

Create a Python program that simulates a bank transaction : if something goes wrong inside the block, the balance goes back to…

Exercise Hard
Solve it

Temporary setting

Create a Python program that changes a global setting only during a with block and restores it on exit, even if there is an error…

Exercise Hard
Solve it

Resource stack

Create a Python program that opens a variable number of resources and closes them all at the end in reverse order. When you do…

Exercise Hard
Solve it

Error-aware log

Create a Python program with a context manager that decides according to the type of error : the parameters of __exit__ tell…

Exercise Hard
Solve it