On Fri, Nov 15, 2019, at 10:30, Joao S. O. Bueno wrote: > Just throwing this idea in: what about an approach _not touching_ the > parser or compiler at all? : > Just add __enter__ and __exit__ to tuples themselves! Instead of > repeating "why should we ever do that", we > _do_ that exactly to enter the contexts in all tuple elements, and > leave then in order.
This may have already been explained already in the thread, but possibly not clearly enough: this does not solve closing the first file if opening the second file fails (or whatever the equivalent is for doing something other than opening a file). with (open(a), open(b)) as fa, fb: would break down into the steps open file a open file b build tuple call tuple.__enter__ assign result to fa and fb when "open file b" fails, the tuple does not exist yet, so it cannot have an __exit__ that will do cleanup for file a. The compiler would have to handle this case specially. While it might indeed be useful to use "with" with a pre-existing safely constructed tuple [whose __enter__ could handle errors in the items' __enter__ calls], it would not solve the problem of cleaning up after an error in the expression initializing one of the items of the tuple. To do this safely, you could use an "opener" context manager that delays actually opening the file until __enter__ is called, but the ability to use it with a plain open call would be dangerously tempting. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/KGDV3IG3UWDLNNXXMHMBA4TINC77R2QL/ Code of Conduct: http://python.org/psf/codeofconduct/