15.11.19 12:40, Jonathan Fine пише:
The original poster wanted, inside 'with' context management, to open several files. Context management is, roughly speaking, deferred execution wrapped in a try ... except .... statement.

In case of open() there is no deferred execution. The resource is acquired in open(), not in __enter__().

The lambda construction also provides for deferred execution. Perhaps something like
 >>>     with helper(lambda: open('a'), open('b'), open('c')) as a, b c:
 >>>         pass
would do what the original poster wanted, for a suitable value of helper. It certainly gives the parentheses the OP wanted, without introducing new syntax.

It does not work. File 'a' will be leaked if opening file 'b' is failed.

However, map also provides for deferred execution. It returns an iterable (not a list).
 >>> x = map(open, ['a', 'b', 'c'])
 >>> x
<map object at 0x7f8cb5653b70>
 >>> next(x)
FileNotFoundError: [Errno 2] No such file or directory: 'a'

So the OP could even write:
 >>>    with helper(map(open, ['a', 'b', 'c'])) as a, b, c:
 >>>        pass
for a (different) suitable value of helper.

It can work (the helper can be implemented using ExitStack), although it looks more clumsy to me than just using line continuations.
_______________________________________________
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/4IFJQ3UKYK54EJDJ2YQ2ARE4QHGARZ34/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to