python-features
How With Statements Compile
Context manager enter and exit in compiled code.
Published May 30, 2026
Enter
__enter__ is called and the result is bound.
Body
The with body compiles normally.
Exit
__exit__ is called on both normal and exception paths.
Nested With
Multiple context managers compile to sequential enter/exit blocks.