One of the 50 questions in this topic
How much do you know about Python?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.