Skip to content
CodeWiki
Practice
Paths
Tracks
Cheatsheets
Playground
Glossary
AI era
Search
⌘K
English
English
Chinese
Practice
Paths
Tracks
Cheatsheets
Playground
Glossary
AI era
English
English
Chinese
Practice
/
Quiz
/
Python
/
Context managers
What must a context manager do if __enter__() fails after acquiring its first subresource?
from Context managers
Python 3.14
intermediate
3 min
What must a context manager do if __enter__() fails after acquiring its first subresource?
Undo the partial acquisition before propagating, because Python will not call its __exit__().
Return True so Python calls __exit__() without exception details.
Rely on garbage collection to run __exit__() immediately.
Store the resource in the as target so the caller can close it.
Check
Ask AI about this kata
Report an error
next kata
Why should dynamic acquisition call stack.enter_context() immediately for each resource?
Quiz
A new version is available
Reload