2 ms·
In particular ExitStacks are useful when you have 0 or 1 resources. It allows a sort of null-context manager for when the number of resources you have to alloc
by jnwatson 6y ago
In particular ExitStacks are useful when you have 0 or 1 resources. It allows a sort of null-context manager for when the number of resources you have to allocate might be 0.
- hchasestevens 6y agoDo be aware that https://docs.python.org/3.9/library/contextlib.html#contextlib.nullcontext https://docs.python.org/3.9/library/contextlib.html#contextl... also exists as of 3.7 -- you can do ctx_mgr = foo() if bar else nullcontext() with ctx_mgr: ... if you like.
- BerislavLopac 6y agoOr even, from Python 3.8: with (ctx_mgr := foo() if bar else nullcontext()): ...