1 min read

Python Context Managers

Context managers allow you to allocate and release resources precisely when you want to. The most widely used example of context managers is the with statement.

The with Statement

The with statement simplifies exception handling by encapsulating common preparation and cleanup tasks.

Example: File Handling

Without a context manager:

file = open('hello.txt', 'w')
try:
    file.write('Hello, world!')
finally:
    file.close()

With a context manager:

with open('hello.txt', 'w') as file:
    file.write('Hello, world!')

The with statement ensures that the file is closed even if an exception occurs during the write operation.

Creating Context Managers

You can create your own context managers to manage resources.

Using a Class

To create a context manager using a class, you need to implement the __enter__() and __exit__() methods.

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 manager:
    print("Inside the context")

Using contextlib

The contextlib module provides a decorator @contextmanager that allows you to define a context manager using a generator function.

from contextlib import contextmanager

@contextmanager
def my_context_manager():
    print("Entering the context...")
    yield
    print("Exiting the context...")

with my_context_manager():
    print("Inside the context")

programming/python/python