[Python-ideas] unpacking context managers in WITH statement

Michael Foord fuzzyman at gmail.com
Fri Feb 3 18:50:06 CET 2012

On 3 February 2012 15:09, Yury Selivanov <yselivanov.ml at gmail.com> wrote:

> Hello,
> With the removal of "contextlib.nested" in python 3.2 nothing was
> introduced to replace it.  However, I found it pretty useful, despite the
> fact that it had its own quirks.  These quirks can (at least partially) be
> addressed by allowing unpacking syntax in the context manager.
> Consider the following snipped of code:
>  ctxs = ()
>  if args.profile:
>      ctxs += (ApplicationProfilerContext(),)
>  if args.logging:
>      ctxs += (ApplicationLoggingContext(),)
>  with *ctxs:
>      Application.run()

Well, I quite like this syntax and it does allow you to do something not
currently easily possible:

with *ctxs as tuple_of_results:

The use case is reasonably obscure however, and should this be possible:

with *ctx, other as tuple_of_results, another:


> As of now, without "nested" we have either option of reimplementing it, or
> to write lots of ugly code with nested 'try..except's.  So the feature was
> taken out, but nothing replaced it.
> What do you think guys?
> Thanks,
> Yury
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120203/4afa057f/attachment.html>

More information about the Python-ideas mailing list