Concept
The with statement guarantees that something is cleaned up, such as a file closed or a lock released, even if the block raises. Any object with __enter__ and __exit__ can be used this way.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Context managers doing its job.
from contextlib import contextmanager
import time
@contextmanager
def timed(label):
start = time.perf_counter()
try:
yield
finally:
print(label, round(time.perf_counter() - start, 3), "s")
with timed("sum"):
total = sum(range(1_000_000))Calling open() without with and relying on the file closing itself. On an exception, or in other Python implementations, the file can stay open and buffered writes may never land.
Your roadmap puts these before Context managers, unless you already know them.
Once a session shows you understand Context managers, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.