DeployU
Interviews / Backend Engineering / What are context managers and how do you use them in Python?

What are context managers and how do you use them in Python?

conceptual Core Concepts Interactive Quiz Code Examples

The Scenario

You are a backend engineer at a fintech company. You are writing a new service that needs to interact with a database. You need to make sure that the database connection is always closed, even if an error occurs.

The Challenge

Explain what context managers are in Python and how you would use them to solve this problem. What are the key benefits of using context managers?

Wrong Approach

A junior engineer might try to solve this problem by using a `try...finally` block to make sure that the database connection is always closed. This would work, but it would be verbose and error-prone.

Right Approach

A senior engineer would know that context managers are the perfect tool for this job. They would be able to explain what context managers are and how to use them to automatically close the database connection, even if an error occurs.

Step 1: Understand What Context Managers Are

A context manager is an object that defines a temporary context for a block of code. It is used with the with statement.

A context manager must have two methods:

  • __enter__(self): This method is called when the with statement is entered. It should return an object that can be used in the with block.
  • __exit__(self, exc_type, exc_value, traceback): This method is called when the with statement is exited. It can be used to perform cleanup operations, such as closing a file or a database connection.

Step 2: Write a Simple Context Manager

Here’s how we can write a simple context manager to manage a database connection:

class DatabaseConnection:
    def __init__(self, db_name):
        self.db_name = db_name

    def __enter__(self):
        self.conn = connect_to_db(self.db_name)
        return self.conn

    def __exit__(self, exc_type, exc_value, traceback):
        self.conn.close()

Step 3: Use the Context Manager

We can use the context manager with the with statement:

with DatabaseConnection('my_db') as conn:
  # ... (your database code) ...

Now, the database connection will be automatically closed when the with block is exited, even if an error occurs.

The contextlib Module

The contextlib module provides a more convenient way to create a context manager using a generator.

from contextlib import contextmanager

@contextmanager
def database_connection(db_name):
    conn = connect_to_db(db_name)
    try:
        yield conn
    finally:
        conn.close()

Practice Question

You are writing a context manager to open a file. In which method should you close the file?