Python multi-line with statement

女生的网名这么多〃 提交于 2019-12-17 18:34:55

问题


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?


回答1:


Given that you've tagged this Python 3, if you need to intersperse comments with your context managers, I would use a contextlib.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.




回答2:


This seems tidiest to me:

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



回答3:


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.




回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/31039022/python-multi-line-with-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!