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 +
    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
    Gerald Britton, Jan 21, 2011
    #1
    1. Advertising

  2. Gerald Britton

    Carl Banks Guest

    On Jan 21, 11:53 am, Gerald Britton <> wrote:
    > 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
    Carl Banks, Jan 21, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,155
    robert
    Feb 11, 2006
  2. Russell Wallace

    Are line continuations needed?

    Russell Wallace, Apr 7, 2004, in forum: Python
    Replies:
    13
    Views:
    490
    Peter Maas
    Apr 8, 2004
  3. Batista, Facundo

    RE: Are line continuations needed?

    Batista, Facundo, Apr 7, 2004, in forum: Python
    Replies:
    9
    Views:
    330
    Duncan Booth
    Apr 9, 2004
  4. bradallen
    Replies:
    3
    Views:
    227
    Raymond Hettinger
    Feb 5, 2010
  5. Lacrima
    Replies:
    1
    Views:
    282
    Chris Rebert
    Apr 1, 2010
Loading...

Share This Page