
July 12, 2019
7:44 p.m.
12.07.19 16:27, haael пише:
Could we add __enter__ and __exit__ to the tuple type?
Look at the following code:
a = open('a.tmp', 'w') b = open('b.tmp', 'w')
with (a, b) as (af, bf): af.write("1") bf.write("2")
Even better example:
with tuple(open(str(_n) + '.tmp', 'w') for _n in range(1000)) as f: for n, fn in enumerate(f): f.write(str(n))
Tuple as context manager would invoke __enter__ for each of its elements and return a tuple of the results.
On exit, the __exit__ method would be invoked for every element.
Are your aware of contextlib.nested()? And why it was deprecated and removed?