[Python-ideas] unpacking context managers in WITH statement

Yury Selivanov yselivanov.ml at gmail.com
Fri Feb 3 16:09:49 CET 2012


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?


More information about the Python-ideas mailing list