0

I would like to break a long statement like:

with X1() as y1, X2() as y2, X3() as y3:  # Really long line
  # Do something

I have tried:

with (
    X1() as y1,
    X2() as y2,
    X3() as y3):
  # Do something

but this is a syntax error. It seems that the following works, but I find it relatively difficult to read:

with X1(
    ) as y1, X2(
    ) as y2, X3(
    ) as y3:
  # Do something

Are there any suggestions on how to best format a long with statement like this? Thanks.

Max
  • 1,670
  • 1
  • 12
  • 17

1 Answers1

3
with X1() as y1, \
    X2() as y2, \
    X3() as y3:

Try that. I think it's important to keep the individual statements as one line. The backslash helps keep the explicit > implicit style of Python.

Reference here: https://www.python.org/dev/peps/pep-0008/

cmglorioso
  • 65
  • 3