Context Managers and Python’s with Statement

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.

Besides, the context management protocol allows you to create your own context managers so you can customize the way you deal with system resources. So, what’s the with statement good for?

In this tutorial, you’ll learn:

  • What the Python with statement is for and how to use it
  • What the context management protocol is
  • How to implement your own context managers

With this knowledge, you’ll write more expressive code and

 

 

 

To finish reading, please visit source site