PEP8, line continuations and string formatting operations

G

Gerald Britton

Style question:

PEP 8 suggests that line continuations be done by enclosing
expressions in parentheses rather than using the line continuation
character. In the same paragraph, it states a preference to put
binary operators at the end of the line to be continued, so:

x = (a +
b)

is preferred over:

x = (a
+ b)

Fair enough.

What about string formatting operations (old style) though? The %
symbols is a binary operator between a string and the substitution
values. Strictly reading PEP 8 leads to:

my_string = ("A long string with %s substitutions that %s the line
should be %s." %
("many", "suggest", "continued")
)

However, I often see the % on the continued line, immediately
preceding the substitution variables, like this:

my_string = ("A long string with %s substitutions that %s the line
should be %s."
% ("many", "suggest", "continued")
)

This goes against the PEP 8 guidelines, but I prefer it since it makes
the substitution variables "jump out" a bit more -- at least to me.

So....what's the general feeling about this? Adhere to the PEP 8
binary operators style, or modify it for string formatting?
 
C

Carl Banks

So....what's the general feeling about this? Adhere to the PEP 8
binary operators style, or modify it for string formatting?

Well, personally I ignore the "operator at end of first line"
guideline altogether; I think it's much more readable with the
operator on the following line, not even close.

I'm starting to not fold lines with parentheses as much, either.
Sometimes the parentheses break the flow too much, or suggest grouping
where it isn't desirable.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top