[Python-ideas] unpacking context managers in WITH statement

Yury Selivanov yselivanov.ml at gmail.com
Fri Feb 3 17:47:29 CET 2012

Well, I bet most of the developers will continue using explicit 
syntax as it is just more convenient.  Unpacking is just a specific
feature to address some specific needs, where the case about
"not executing constructor in case of parent context fault" may not
be applicable.  "With" statement is far more now than just about
opening files after all ;)

On 2012-02-03, at 11:35 AM, Mathias Panzenböck wrote:

> 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
> _______________________________________________
> 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