Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multi-line with statement

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?

like image 344
Justin Avatar asked Jun 24 '15 23:06

Justin


People also ask

How do you do a multiple line statement in Python?

Multi-line Statement in Python: In Python, the statements are usually written in a single line and the last character of these lines is newline. To extend the statement to one or more lines we can use braces {}, parentheses (), square [], semi-colon “;”, and continuation character slash “\”.

What is statement and multi-line statement in Python?

PythonServer Side ProgrammingProgramming. Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example − total = item_one + \ item_two + \ item_three.

How do you write a Python statement that can expand over multiple lines?

Usually, every Python statement ends with a newline character. However, we can extend it over to multiple lines using the line continuation character (\).

Can I have multiple IF statements in Python?

It works that way in real life, and it works that way in Python. if statements can be nested within other if statements. This can actually be done indefinitely, and it doesn't matter where they are nested. You could put a second if within the initial if .


2 Answers

As of Python 3.10, it is now possible to parenthesize the whole group of context managers, as you originally tried:

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

This is also technically possible in 3.9, but in a sort of semi-documented limbo.

On one hand, it's documented as new in 3.10, 3.9 wasn't supposed to introduce any features (like this one) that depend on the new parser implementation, and the 3.9 with docs forbid this form. On the other hand, the functionality ended up getting activated in the 3.9 CPython implementation, and the (mostly?) auto-generated 3.9 full grammar spec includes the parenthesized form.


On previous Python 3 versions, 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):     ... 

before the new parser implementation is because a ( can also be the first token of the expression for a context manager, and CPython's old 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's new PEG-based parser.

like image 110
user2357112 supports Monica Avatar answered Sep 29 '22 07:09

user2357112 supports Monica


Python 3.9+ only:

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

Python ≤ 3.8:

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

Unfortunately, comments are not possible with this syntax.

like image 44
Neil G Avatar answered Sep 29 '22 07:09

Neil G