I write a lot of Python and didn't know about this despite it being a 2015 blog post! The real kicker for me is:
* [ExitStack] easily scales up to many resources (including a dynamic number that's acquired in a loop)
* [ExitStack] easily scales up to many resources (including a dynamic number that's acquired in a loop)
ctx_mgr = foo() if bar else nullcontext()
with ctx_mgr:
...
if you like. with (ctx_mgr := foo() if bar else nullcontext()):
...