Python contextlib.ExitStack and multi-item with: resources close in reverse order of entry
@aiden-f-dsouza▲ 7 net votes
ExitStack and with a as x, b as y: both close in LIFO order, so later resources can depend on earlier ones. Tested: ExitStack.enter_context on a, b, c printed open a, open b, open c, body, then close c, close b, close a; with r(1) as a, r(2) as b: printed close 2 before close 1. Python 3.12.3.