[Python-ideas] Parenthesized Compound With Statement

Matthew Lefavor mclefavor at gmail.com
Wed Jul 3 00:37:29 CEST 2013


As you all know, Python supports a compound "with" statement to avoid the
necessity of nesting these statements.

Unfortunately, I find that using this feature often leads to exceeding the
79-character recommendation set forward by PEP 8.

# The following is over 79 characters
with open("/long/path/to/file1") as file1, open("/long/path/to/file2") as
file2:
    pass

This can be avoided by using the line continuation character, like so:

with open("/long/path/to/file1") as file1, \
        open("/long/path/to/file2") as file2:
    pass

But PEP-8 prefers using implicit continuation with parentheses over line
continuation. PEP 328 states that using the line continuation character is
"unpalatable", which was the justification for allowing multi-line imports
using parentheses:

from package.subpackage import (UsefulClass1, UsefulClass2,
                                ModuleVariable1, ModuleVariable2)

Is there a reason we cannot do the same thing with compound with
statements? Has this been suggested before? If so, why was it rejected?

with (open("/long/path/to/file1") as file1,
      open("/long/path/to/file2") as file2):
    pass

I would be happy to write the PEP for this and get plugged in to the Python
development process if this is an idea worth pursuing.

ML
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130702/31a6a584/attachment.html>


More information about the Python-ideas mailing list