Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Context Managers and Python's with Statement (Overview)

The with statement in Python is a quite useful tool for properly managing external resources in your programs. It allows you to take advantage of existing context managers to automatically handle the setup and teardown phases whenever you’re dealing with external resources or with operations that require those phases.

What’s a context manager? It’s a block of code that has side effects upon entering and exiting. The context management protocol allows you to create your own context managers so you can customize the way you deal with system resources.

In this video course, you’ll learn:

  • How context managers work
  • Some common context managers in the Python standard library
  • How to write a custom context manager

With this knowledge, you’ll write more expressive code and avoid resource leaks in your programs.

Download

Sample Code (.zip)

7.7 KB

Download

Course Slides (.pdf)

1.8 MB

00:00 Welcome to Context Managers and Python’s with Statement. My name is Christopher, and I will be your guide. This course is all about a context manager.

00:09 That’s a code block that does resource management using the with keyword.

00:14 In this course, you will learn about how context managers work, common context manager usage in the standard library, and how to write your own context manager.

00:27 A common part of writing programs is managing resources, such as files, network connections, or database connections. The typical interaction is to open one of these resources, do some work, then close the resource. That last part is important.

00:43 You have to remember to clean up after yourself. Your program can actually crash if you leave too many of these kinds of things open at a time. One way of making sure things get cleaned up, even if there’s an exception, is to use a finally section in a try block.

00:59 Or, as you might have guessed from the course’s title, another option is to use a context manager. A context manager is instantiated using the with keyword.

01:09 Underneath the with is a block of code. When that block exits, a special method gets called on the context manager, mostly used to do resource cleanup.

01:20 Context managers can be found throughout the standard library, and they are often one of two ways of doing things. You can use the open() built-in function inside of a tryfinally block, or you can use the same function as a context manager. In the latter case, the file gets closed automatically when the block finishes.

01:41 You can even write your own context managers, either through the creation of a custom class or by writing a function that uses the context manager decorator in the contextlib library.

01:54 Next up, I’ll show you a bunch of examples of context managers in the standard library and how you might use them.

Become a Member to join the conversation.