[Python-ideas] unpacking context managers in WITH statement
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:
> 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:
> 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?
> Python-ideas mailing list
> Python-ideas at python.org
More information about the Python-ideas