[Python-ideas] with *context_managers:
sven at marnach.net
Mon Apr 2 17:34:52 CEST 2012
Ram Rachum schrieb am Sun, 01. Apr 2012, um 13:25:41 -0700:
> I'd like to be able to do this:
> with *context_managers:
> pass # Some suite.
> This is useful when you have an unknown number of context managers that you
> want to use. I currently use `contextlib.nested`, but I'd like the *star
> syntax much better.
'contextlib.nested()' is broken and not available in Python 3.x (the
language this list is about). The only replacement so far is
with CM1() as cm1, CM2() as cm2:
which only works for a fixed number of context managers. And there is
a class 'ContextStack' in Nick Coghlan's 'contextlib2' library ,
which might be included in Python 3.3. With this class, you could
write your code as
with ContextStack() as stack:
for cm in context_managers:
This still leaves the question whether your proposed syntax would be
preferable, also with regard to issue 2292 .
More information about the Python-ideas