culTest

    One of the 50 questions in this topic

    How much do you know about Python?

    What is a context in Python (used with 'with')?

    Answers

    An indented code block
    An object that defines __enter__() and __exit__() methodsCorrect
    A special type of function
    An isolated namespace

    A context (or context manager) is an object that implements the __enter__() and __exit__() methods, used with the 'with' statement to automatically configure and clear resources. The most common example is opening files: with open('file.txt') as f: ensures that the file is closed even if exceptions occur. Other uses include database connections, locks, timers and transactions, simplifying resource management and preventing leaks.