[Python-ideas] Parenthesized Compound With Statement

Ron Adam ron3200 at gmail.com
Wed Jul 3 18:56:09 CEST 2013



On 07/03/2013 10:01 AM, Barry Warsaw wrote:
> As for relaxing PEP 8, well, I'm not sure what more you'd want it to say.  It
> currently reads:
>
>      The preferred way of wrapping long lines is by using Python's implied
>      line continuation inside parentheses, brackets and braces.  Long lines
>      can be broken over multiple lines by wrapping expressions in
>      parentheses. These should be used in preference to using a backslash
>      for line continuation.
>
> which seems about right to me.  It is preferred to use implied continuation
> over backslashes, but doesn't say "don't use backslashes".  They're not evil,
> just not preferred, so use them where appropriate and with good judgment.


I think this can be improved on.  The second part should be a positive 
statement expressing when backslashes should be used over an implied 
continuation, rather than a blanket negative statement.

     Use a backslash if parentheses cause the code to be less clear.




My own preference is:

Use what obviously reads better.  This can happen when continuing 
expressions which contain tuples, or strings with backslashes.  Using what 
is not in the expression can help make the intention clearer.

Use what takes less characters.  For an expression that span 2 lines, use a 
single backslash. For longer expressions spanning 4 or more lines 
parentheses are preferred.

For continued lines inside of function calls, and container literals, 
indenting the continued lines is enough in most cases.

Sometimes adding a backslash within a container can make a continued line 
between many single line items stand out better. (*)


(* There have been "silent" bugs where a comma was omitted in cases where 
some lines are continued and some aren't.)

Cheers,
    Ron






More information about the Python-ideas mailing list