What Are Context Managers in Python?

What is a Context Manager in Python?

According to the Python glossary, a context manager is —

An object which controls the environment seen in a with statement by defining enter() and exit() methods.

One of the most common tasks that you'll have to perform in your programs is working with external resources. These resources can be files on your computer's storage or an open connection to third-party service on the internet.

For the sake of simplicity, imagine a program that opens a file, writes something to it, and then closes the file.

Read more on original post: https://www.freecodecamp.org/news/context-managers-in-python

Tags:
#python #dive-in