PEP8, line continuations and string formatting operations

Discussion in 'Python' started by Gerald Britton, Jan 21, 2011.

  1. 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 +

    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, Jan 21, 2011
    1. Advertisements

  2. Gerald Britton

    Carl Banks Guest

    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
    Carl Banks, Jan 21, 2011
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.