I know that the with
statement supports multiple context managers on the same level, like so:
with open('foo.txt') as foo, open('bar.txt') as bar:
do_something()
But what if the list of context managers is getting too long for a single line? e.g.:
with open('foo.txt') as foo, open('bar.txt') as bar, open('bla.txt') as bla, open('yada.txt') as yada:
do_something()
Currently, these are invalid syntax in Python 3.7:
with (
open('foo.txt') as foo,
open('bar.txt') as bar,
open('bla.txt') as bla,
open('yada.txt') as yada, # same thing without the last trailing comma
):
do_something()
with
open('foo.txt') as foo,
open('bar.txt') as bar,
open('bla.txt') as bla,
open('yada.txt') as yada, # same thing without the last trailing comma
:
do_something()
I could do:
foo = open('foo.txt')
bar = open('bar.txt')
bla = open('bla.txt')
yada = open('yada.txt')
with foo, bar, bla, yada:
do_something()
But even THAT could grow too long as I add more context managers.
I could also do:
with open('foo.txt') as foo:
with open('bar.txt' as bar:
with open('bla.txt' as bla:
with open('yada.txt') as yada:
do_something()
But it's ugly. It also lacks semantic hints for the human reader. There's a reason why we would want to put multiple context managers on the same level in the first place.
I know it's very uncommon for many context managers to belong to the same level, but it's definitely a possibility.