Skip to content
On this page

Context Managers:

Context managers are used to manage resources using the with statement.

python
# Context Managers


class MyContextManager:
    def __enter__(self):
        print("Entering the context.")
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        print("Exiting the context.")

with MyContextManager() as cm:
    print("Inside the context.")