On Sun, Nov 4, 2012 at 5:41 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
Yep. You can also do some pretty interesting things with ExitStack
because of the pop_all() operation (which moves all of the registered
operations to a *new* ExitStack instance).

I wrote up a few of the motivating use cases as examples and recipes
in the 3.3 docs:
http://docs.python.org/3/library/contextlib#examples-and-recipes

I hope to see more interesting uses over time as more people explore
the possibilities of a dynamic tool for composing context managers
without needing to worry about the messy details of unwinding them
correctly (ExitStack.__exit__ is by far the most complicated aspect of
the implementation).

Cheers,
Nick.


Pretty interesting things indeed. It does look like the concept is very powerful. If I were to design a language I might have just given every function an optional ExitStack. I.e. an explicit, dynamic, introspectable version of defer.