
On 2019-11-18 9:10 a.m., Oscar Benjamin wrote:
[snip]
To me that seems clumsy and awkward compared to nested though:
with nested(*map(open, filenames)) as files: ...
Ideally I would design nested to take an iterable rather than *args and then it would be fine to do e.g.
with nested(open(filename) for filename in filenames) as files: ...
Here nested could take advantage of the delayed evaluation in the generator expression to invoke the __enter__ methods and call __exit__ on the opened files if any of the open calls fails. This would also leave a "trap" though since using a list comprehension would suffer the same problem as if nested took *args:
with nested([open(filename) for filename in filenames]) as files: ...
If generator expressions (aka "(open(filename) for filename in filenames)") had __enter__ and __exit__ that deferred to inner __enter__ and __exit__, this "trap" wouldn't exist: with (open(filename) for filename in filenames) as files: ... # fine with [open(filename) for filename in filenames] as files: ... # raises because list doesn't __enter__ mainly because it wouldn't work with arbitrary iterators or iterables. (and if you need it to, "with (x for x in iterable)" would still be available)
[snip]
Oscar _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/KIYSRW... Code of Conduct: http://python.org/psf/codeofconduct/