52
votes

What is a clean way to create a multi-line with in python? I want to open up several files inside a single with, but it's far enough to the right that I want it on multiple lines. Like this:

class Dummy:
    def __enter__(self): pass
    def __exit__(self, type, value, traceback): pass

with Dummy() as a, Dummy() as b,
     Dummy() as c:
    pass

Unfortunately, that is a SyntaxError. So I tried this:

with (Dummy() as a, Dummy() as b,
      Dummy() as c):
    pass

Also a syntax error. However, this worked:

with Dummy() as a, Dummy() as b,\
     Dummy() as c:
    pass

But what if I wanted to place a comment? This does not work:

with Dummy() as a, Dummy() as b,\
     # my comment explaining why I wanted Dummy() as c\
     Dummy() as c:
    pass

Nor does any obvious variation on the placement of the \s.

Is there a clean way to create a multi-line with statement that allows comments inside it?

6
Actually, the big question is what PEP-8 says about this stuff, since PEP-8 restricts line-length to 80 chars, which is what makes doing this necessary. - Justin
Opinion time: PEP-8 is great, but I think the 80-character limit is too low. It's based on the old terminal limitations, not human usability. I'm not advocating lines with 300+ characters, but I have no problem with 120-130 or so. - TigerhawkT3
@TigerhawkT3 I think that the 80 char limit is low too, but I see a benefit to it when I'm working on a project that requires me to have 5 files open simultaneously. It's much easier to be able to see every file. I might make an exception for this file, though. - Justin
PEP-8 is explicitly ok with ` \ ` line continuation for multiline with statements, since you can't use implicit continuation. That doesn't really help your situation if you want to inline comments, though. - Eric Appelt

6 Answers

8
votes

This is fixed in Python 3.9!

https://github.com/we-like-parsers/pegen/issues/229

with (Dummy() as a,
        Dummy() as b,
        # my comment explaining why I wanted Dummy() as c
        Dummy() as c):
    pass

Here's proof that it works:

Python 3.9.0a6 (default, Jun 20 2020, 14:52:53) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.15.0 -- An enhanced Interactive Python. Type '?' for help.

In [3]: with (open('x') 
   ...:        as f): 
   ...:     pass 
   ...:                                                                                                                                                                                                 
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-47d5a51db08b> in <module>
----> 1 with (open('x')
      2        as f):
      3     pass
      4 

FileNotFoundError: [Errno 2] No such file or directory: 'x'

In [4]:                                                                                                                                                                                                 
Do you really want to exit ([y]/n)? y

wPython 3.8.2 (default, May  8 2020, 20:08:31) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.15.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: with (open('x') 
   ...:       as f): 
   ...:     pass                                                                                                                                                                                        
  File "<ipython-input-1-e538abd13934>", line 2
    as f):
    ^
SyntaxError: invalid syntax
54
votes

Given that you've tagged this Python 3, if you need to intersperse comments with your context managers, I would use a contextlib.ExitStack:

from contextlib import ExitStack

with ExitStack() as stack:
    a = stack.enter_context(Dummy()) # Relevant comment
    b = stack.enter_context(Dummy()) # Comment about b
    c = stack.enter_context(Dummy()) # Further information

This is equivalent to

with Dummy() as a, Dummy() as b, Dummy() as c:

This has the benefit that you can generate your context managers in a loop instead of needing to separately list each one. The documentation gives the example that if you want to open a bunch of files, and you have the filenames in a list, you can do

with ExitStack() as stack:
    files = [stack.enter_context(open(fname)) for fname in filenames]

If your context managers take so much screen space that you want to put comments between them, you probably have enough to want to use some sort of loop.


As Mr. Deathless mentions in the comments, there's a contextlib backport on PyPI under the name contextlib2. If you're on Python 2, you can use the backport's implementation of ExitStack.


Incidentally, the reason you can't do something like

with (
        ThingA() as a,
        ThingB() as b):
    ...

is because a ( can also be the first token of the expression for a context manager, and CPython's current parser wouldn't be able to tell what rule it's supposed to be parsing when it sees the first (. This is one of the motivating examples for PEP 617, which introduces a much more powerful new parser, so the syntax you wanted may soon exist.

13
votes

This seems tidiest to me:

with open('firstfile', 'r') as (f1 # first
  ), open('secondfile', 'r') as (f2 # second
  ):
    pass
3
votes

This isn't exactly clean, but you could do this:

with Dummy() as a, Dummy() as b, (
     #my comment
     Dummy()) as c:
    pass

There are no syntax errors, but it's not the cleanest. You could also do this:

with Dummy() as a, Dummy() as b, Dummy(
     #my comment
     ) as c:
    pass

Consider finding a way of doing this without using the comments in the middle of the with.

3
votes

I would keep things simple and readable by adding the comment before the with statement, or on the line itself:

# my comment explaining why I wanted Dummy() as c
with Dummy() as a, Dummy() as b,\
     Dummy() as c: # or add the comment here
    pass
0
votes

Like TigerhawkT3's answer, but with indenting that doesn't trigger pycodestyle's error E124:

with (
        open('firstfile', 'r')) as f1, (  # first
        open('secondfile', 'r')) as f2:  # second
    pass

IMO it's still ugly, but at least it passes the linter.