16. Short Q&A Prompts
Practice prompts to sharpen depth and articulation under time pressure.
Describe the iterator protocol and how a generator satisfies it. When would you use
yield from?Show a context manager built with
@contextmanagerand contrast with a class-based CM.Write a small decorator that preserves metadata with
functools.wraps.Explain
dataclass(slots=True, frozen=True)trade-offs.Demonstrate
match/casewith a mapping pattern and a guard (if ...).Pick between threads, processes, and asyncio for a given workload and justify.
Avoid N+1 in SQLAlchemy:
selectinloadvsjoinedload— when each?pytestfixtures: scope and parametrization examples.Design idempotent POST with
Idempotency-Key.Prevent a cache stampede: outline lock/jitter/refresh-ahead.
Show
asyncio.Semaphoreto cap concurrency.Implement optimistic locking with a version column.
Use
pytestmonkeypatchto override an env var.Explain
ExceptionGroupand handle withexcept*.Add ETag-based conditional GET to a REST endpoint.