<p dir="ltr"><br>
On 12 Aug 2014 09:09, "Allen Li" <<a href="mailto:cyberdupo56@gmail.com">cyberdupo56@gmail.com</a>> wrote:<br>
><br>
> This is a problem I sometimes run into when working with a lot of files<br>
> simultaneously, where I need three or more `with` statements:<br>
><br>
>     with open('foo') as foo:<br>
>         with open('bar') as bar:<br>
>             with open('baz') as baz:<br>
>                 pass<br>
><br>
> Thankfully, support for multiple items was added in 3.1:<br>
><br>
>     with open('foo') as foo, open('bar') as bar, open('baz') as baz:<br>
>         pass<br>
><br>
> However, this begs the need for a multiline form, especially when<br>
> working with three or more items:<br>
><br>
>     with open('foo') as foo, \<br>
>          open('bar') as bar, \<br>
>          open('baz') as baz, \<br>
>          open('spam') as spam \<br>
>          open('eggs') as eggs:<br>
>         pass</p>
<p dir="ltr">I generally see this kind of construct as a sign that refactoring is needed. For example, contextlib.ExitStack offers a number of ways to manage multiple context managers dynamically rather than statically.</p>

<p dir="ltr">Regards,<br>
Nick.</p>