Context Manager
Domain: Programming Fundamentals
π― Motivation
Managing resources (files, connections) safely is error-prone. Context managers (the with statement) handle setup and cleanup automatically.
π Concrete Example
with open('data.txt') as f: data = f.read() - the file is automatically closed when the block ends, even if an error occurred.
π Analogy
A self-cleaning paint roller - when youβre done painting (exit the with block), it automatically cleans itself. You donβt have to remember to clean up.