Calmcode - context managers: introduction

Context Managers in Python

1 2 3 4 5 6

In programming, much like in real life, it's important to clean up our toys after we're done playing with them. For example, forgetting to close a connection to a database can waste valuable resources on your database server, and forgetting to close a file might result in data loss, or your operating system refusing to open new files in your program.

# set up a thing
connection = open_db_connection()

# use a thing
run_analysis(connection)

# clean up the thing
connection.close()

There is a problem in the code snippet above though. What happens if run_analysis throws an Exception, and crashes your program? In that case, the connection.close() line will never get executed, and your database server won't know the connection can be closed

In Python, we can use a try ... finally block to deal with this issue. In contrast to an except block, which only runs in case the try block throws an exception, The code in the finally block will always be executed, whether the try block threw an exception or not

# set up a thing
connection = open_db_connection()

try:
    # use a thing
    run_analysis(connection)
finally:
    # clean up the thing
    connection.close()

One of the downsides of this approach is that the 'set up' of the connection and the 'clean up' of the connetion are separated by a (potentially quite large) block of code. This makes it quite easy to do the 'wrong' thing and forget to close the connection.

Python tries to make it hard to do the wrong thing, and to that end, PEP-310 first introduced a bit of syntax to the language that made it impossible to forget to clean up resources, the with statement. Using this with statement (also known as a context manager), the code snippet above can be simplified to:

with open_db_connection() as connection:
    run_analysis(connection)

Once the with block is entered, the connection gets created and stored in the connection variable. Then, the body of the with block gets executed, after which the connection will get closed by the context manager automatically, no matter whether run_analysis threw an exception or not.

The goal of this series of video is to explain these "context manager"-blocks in more detail.