Zoran Pandovski
By Zoran Pandovski

Python context managers

Python context managers

Have you ever heard about Context managers in Python and you weren’t sure what they are? The name may be strange but I am sure you have already used one. Let’s see one example for reading the file in Python:

1
2
3
4
5
6
7
8
9
10
11
12
13
    f = open(filename)

    try:

        yield f

    finally:

        f.close()



In this implementation, the file wouldn’t be closed if the exception happened. Let’s another example that uses the with statement.

1
2
3
4
5
with open('file.txt') as f:

    content = f.read()

This code example is clean, easier to read and you are sure that the file method close will be called after execution leaves the with context. So, that is the job of the Context Manager. Context managers (objects that perform context management) allows you to release or allocate the resources when you want and can help you to avoid resource leaks. The contextlib module provides utilities for most of the tasks where the with is used. To learn how to create your own context managers check out .