WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Guru · question 88 of 100

How do you design and implement custom decorators and context managers for advanced resource management and code transformation in Python?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Decorators and context managers are powerful Python features that allow for advanced resource management and code transformation. In this answer, we’ll discuss how to design and implement custom decorators and context managers in Python, with examples.

Custom Decorators

Decorators are functions that modify the behavior of other functions. They take a function as input, and return a new function that wraps the original function. Custom decorators can be used to add functionality to functions without modifying the original function code. This can be useful for implementing cross-cutting concerns, such as logging, timing, and caching.

Here’s an example of a custom decorator that logs the start and end time of a function:

    import functools
    import logging
    import time
    
    def timed(fn):
        @functools.wraps(fn)
        def wrapper(*args, **kwargs):
            start_time = time.time()
            result = fn(*args, **kwargs)
            end_time = time.time()
            logging.info(f"Function {fn.__name__} took {end_time - start_time} seconds to run")
            return result
        return wrapper
    
    @timed
    def my_function():
        # do some work
        pass

In this example, the timed decorator takes a function as input, and returns a new function wrapper that wraps the original function fn. The wrapper function logs the start and end time of the original function, and returns its result. The @timed decorator is applied to the my_function function, which adds the timing functionality to it.

Custom Context Managers

Context managers are objects that define a context in which a block of code is executed. They provide a way to manage resources, such as files, locks, and connections, in a safe and consistent way. Custom context managers can be used to implement custom resource management, such as transaction management, error handling, and memory management.

Here’s an example of a custom context manager that manages a file resource:

    class FileManager:
        def __init__(self, filename, mode):
            self.filename = filename
            self.mode = mode
            self.file = None
        
        def __enter__(self):
            self.file = open(self.filename, self.mode)
            return self.file
            
        def __exit__(self, exc_type, exc_value, traceback):
            self.file.close()
    
    with FileManager("data.txt", "w") as f:
        f.write("hello, world!")

In this example, the FileManager class defines a context manager that manages a file resource. The __enter__ method is called when the with statement is entered, and it opens the file and returns it. The __exit__ method is called when the with statement is exited, and it closes the file. The with statement is used to manage the file resource, and ensure that it is closed properly.

Custom decorators and context managers are powerful Python features that can be used to implement advanced resource management and code transformation. By defining custom decorators and context managers, you can add new functionality to your code in a safe and consistent way, and improve its maintainability and readability.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics