PEP8, line continuations and string formatting operations

Gerald Britton gerald.britton at gmail.com
Fri Jan 21 14:53:55 EST 2011


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?


-- 
Gerald Britton



More information about the Python-list mailing list