[Python-ideas] unpacking context managers in WITH statement

Mathias Panzenböck grosser.meister.morti at gmx.net
Fri Feb 3 17:35:10 CET 2012


Oh, wait. You do something a bit different. Hm, yes, when you have a list of context managers its 
something different. Still, I'm not sure if it is a good thing to do it like you've proposed. After 
all, usually the constructor of a nested context manager shall only be called if the parent context 
could be entered. You would construct all context managers before you enter any. Maybe it's ok for 
for your case, but it might send the wrong signal to the developers and might be used like nested 
was (see my other mail).

On 02/03/2012 04:09 PM, Yury Selivanov 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()
>
> 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
>




More information about the Python-ideas mailing list