I would not recommend ExitStack for this scenario -- it's meant for
situations where the cleanup is *dynamic* (see examples in the docs:
https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack).

On Wed, Nov 13, 2019 at 10:53 AM James Edwards <jh...@jheiv.com> wrote:

> The syntax error is coming from finding "as" in a place it's unexpected.
> (Additionally, if you were to drop the `as fn`, you'd get an AttributeError
> as tuple.__enter__ isn't defined).
>
> There's a contextlib helper that you might consider:
> https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack
>
> Which you might use like:
>
>     from contextlib import ExitStack
>
>     with ExitStack() as stack:
>         f1 = stack.enter_context(open(fname1))
>         f2 = stack.enter_context(open(fname2))
>         f3 = stack.enter_context(open(fname3))
>         f4 = stack.enter_context(open(fname4))
>         ...
>
> On Wed, Nov 13, 2019 at 1:30 PM <gabriel.ka...@mail.de> wrote:
>
>> Hello everybody,
>>
>> today I tried to open four files simultaneously by writing
>>
>> with (
>>     open(fname1) as f1,
>>     open(fname2) as f2,
>>     open(fname3) as f3,
>>     open(fname4) as f4
>> ):
>>     ...
>>
>> However, this results in a SyntaxError which is caused by the extra
>> brackets. Is there a reason that brackets are not allowed in this place?
>> _______________________________________________
>> 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/7GRCN5RNRWPDJ7TWJF7MK4ARSS27DK6Y/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
> _______________________________________________
> 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/VLCRQ7EKPMR6XCYIZLTOHC7K4VIJTPXT/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
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/MGWLE2LJQUEAR57T327LI5OXLEJSPI7C/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to