python multiple items in with statment

Solutions on MaxInterview for python multiple items in with statment by the best coders in the world

showing results for - "python multiple items in with statment"
Luca
30 Jul 2018
1# It is possible in Python 3 since v3.1 and Python 2.7. The
2#	new with syntax supports multiple context managers:
3with A() as a, B() as b, C() as c:
4    doSomething(a,b,c)
5# Unlike the contextlib.nested,
6#	this guarantees that a and b will have their __exit__()'s called even if C() or it's __enter__() method raises an exception.
7# You can also use earlier variables in later definitions:
8with A() as a, B(a) as b, C(a, b) as c:
9    doSomething(a, c)