Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Context Managers and Python's with Statement (Summary)

The Python with statement is a powerful tool when it comes to managing external resources in your programs. Its use cases, however, aren’t limited to resource management. You can use the with statement along with existing and custom context managers to handle the setup and teardown phases of a given process or operation.

The underlying context management protocol allows you to create custom context managers and factor out the setup and teardown logic so you can reuse them in your code.

In this video course, you learned:

  • What the Python with statement is for and how to use it
  • Why you should use context management in your scripts
  • How to implement your own context managers

The recent release of Python 3.11 included a new context manager within the asyncio library called TaskGroup. For more information about this context manager and how it is used, check out the Python 3.11: Cool New Features for You to Try tutorial or course.

With this knowledge, you’ll write safe, concise, and expressive code. You’ll also avoid resource leaks in your programs.


Sample Code (.zip)

7.7 KB

Course Slides (.pdf)

1.8 MB

00:00 In the previous lesson, I showed you two different ways to write a context manager. In this lesson, I’ll summarize the course.

00:07 This course has been all about context managers. Context managers allow you to write blocks of code that have side effects upon entering and exiting them.

00:16 This is particularly useful for managing resources such as file handles, ensuring that you properly close them without having to remember to do so explicitly. Context managers are often used instead of a try finally block. Their advantage over a try block is you don’t have to explicitly clean up connections.

00:35 So the default behavior of your code is the right behavior. You’ve got a little less control over exception-processing with a context manager unless you’re writing a custom one for yourself.

00:46 So you can choose your preferred style based on your use case. To write your own context manager, you write a class that has a .__enter__() and .__exit__() method.

00:56 These methods are called upon entering and exiting the code block, respectively. .__exit__() takes arguments, giving you access to any exception that happens in the code block, and you can decide to swallow the exception inside .__exit__() by returning True.

01:14 You can also write a context manager as a function by using the @contextmanager decorator and the yield keyword.

01:22 This course was recorded a few weeks after the release of Python 3.11. One of the new additions in this release was another context manager called TaskGroup.

01:31 If you think back to the threading example where I created a bunch of threads in a loop, this is something kind of similar. When coding with asyncio, instead of using threads directly, you use coroutines. Like with the .start() and .join() of threads, coroutines require a creation and gathering step.

01:49 The new TaskGroup context manager does the gathering part on exit. It makes the code a little bit shorter and ensures you don’t forget that step.

01:59 For more details on this feature, see either the tutorial article or the course that talks about Python 3.11.

02:08 That’s all for now. I hope you found this course useful. Thanks for your attention.

Become a Member to join the conversation.