Python·100 questions

What is a context manager and with?

Answer
with guarantees resource release.
Files, locks, connections.
Custom manager: implement __enter__ and __exit__.
Or use contextlib.contextmanager.
Simplifies safe code.
Was this answer helpful?

More questions in this topic

Related questions from other topics