On 12 Aug 2014 09:09, "Allen Li" <cyberdup...@gmail.com> wrote:
>
> This is a problem I sometimes run into when working with a lot of files
> simultaneously, where I need three or more `with` statements:
>
>     with open('foo') as foo:
>         with open('bar') as bar:
>             with open('baz') as baz:
>                 pass
>
> Thankfully, support for multiple items was added in 3.1:
>
>     with open('foo') as foo, open('bar') as bar, open('baz') as baz:
>         pass
>
> However, this begs the need for a multiline form, especially when
> working with three or more items:
>
>     with open('foo') as foo, \
>          open('bar') as bar, \
>          open('baz') as baz, \
>          open('spam') as spam \
>          open('eggs') as eggs:
>         pass

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.

Regards,
Nick.
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to