A quite common use-case for
contextlib.ExitStack is to support variable number of
with contextlib.ExitStack() as stack:
for resource in resources:
However this feel quite verbose. When the output of the
__enter__ method is not needed, it would be nice if the above code could be simplified as:
It is a very minor change but I feel would reduce boilerplate in quite a few common places, like:
contextlib.nested has documented limitations and is deprecated, so the initial request makes sense.
What would the implementation look like? It would be strange to call
__init__, so save the managers internally and enter them in
The requested feature has exactly the same design flaws as
contextlib.nested. It would be unsafe to revive
contextlib.nested under other name.
Unless I am misunderstanding something, the proposed feature actually differs from
nested in one specific way: it accepts an iterator, which may be lazy.
with nested(*(open(f) for f in files)):
with ExitStack(open(f) for f in files):
nested version needs to materialize all the items to pass them as arguments, while the
ExitStack version can save the lazy iterator and then lazily consume it in its
Of course, there is still a problem that you could accidentally do something like
with ExitStack([open(f) for f in files]):
resulting in the same incorrect behavior as
Unless we have a way to detect whether the passed iterator is lazy, I am inclined to agree, that this proposal has the same problem as