Multiple variables in a 'with' statement?
with A() as a, B() as b, C() as c: doSomething(a,b,c)
contextlib.nested, this guarantees that
b will have their
__exit__()'s called even if
C() or it's
__enter__() method raises an exception.
You can also use earlier variables in later definitions (h/t Ahmad below):
with A() as a, B(a) as b, C(a, b) as c: doSomething(a, c)
Note that if you split the variables into lines, you must use backslashes to wrap the newlines.
with A() as a, \ B() as b, \ C() as c: doSomething(a,b,c)
Parentheses don't work, since Python creates a tuple instead.
with (A(), B(), C()): doSomething(a,b,c)
Since tuples lack a
__enter__ attribute, you get an error (undescriptive and does not identify class type):
If you try to use
as within parentheses, Python catches the mistake at parse time:
with (A() as a, B() as b, C() as c): doSomething(a,b,c)
SyntaxError: invalid syntax
When will this be fixed?
This issue is tracked in https://bugs.python.org/issue12782.
Recently, Python announced in PEP 617 that they'll be replacing the current parser with a new one. Because Python's current parser is LL(1), it cannot distinguish between "multiple context managers"
with (A(), B()): and "tuple of values"
with (A(), B()):.
The new parser can properly parse multiple context managers surrounded by parentheses. The new parser has been enabled in 3.9, but this syntax will still be rejected until the old parser is removed in Python 3.10.
contextlib.nested supports this:
import contextlibwith contextlib.nested(open("out.txt","wt"), open("in.txt")) as (file_out, file_in): ...
To quote the documentation, regarding
Deprecated since version 2.7: The with-statement now supports this functionality directly (without the confusing error prone quirks).
See Rafał Dowgird's answer for more information.