Python line break in long "with x1 as y1, x2 as y2, x3 as y3" statement [duplicate]

This question already has an answer here:

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.

Source: syntax

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.