[Python-ideas] Parenthesized Compound With Statement

Guido van Rossum guido at python.org
Wed Jul 3 05:04:56 CEST 2013


It would be easier to update PEP 8 to be less dogmatic about
backslashes. Where parentheses work equally well, I prefer them. But
for this and similar cases (asserts come to mind) I don't see why you
can't use a backslash. I would certainly prefer that over a syntax
change.

On Tue, Jul 2, 2013 at 3:37 PM, Matthew Lefavor <mclefavor at gmail.com> wrote:
> 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list