Are line continuations needed?

Discussion in 'Python' started by Russell Wallace, Apr 7, 2004.

  1. Hi all,

    Python lets you continue a single logical line across more than one
    physical line, either by putting a \ at the end or letting it happen
    automatically with an incomplete infix operator.

    I'm wondering how often is this feature needed? Would there be any
    problems if it weren't part of the language?

    --
    "Sore wa himitsu desu."
    To reply by email, remove
    the small snack from address.
    http://www.esatclear.ie/~rwallace
     
    Russell Wallace, Apr 7, 2004
    #1
    1. Advertising

  2. Russell Wallace wrote:
    > Hi all,
    >
    > Python lets you continue a single logical line across more than one
    > physical line, either by putting a \ at the end or letting it happen
    > automatically with an incomplete infix operator.
    >
    > I'm wondering how often is this feature needed? Would there be any
    > problems if it weren't part of the language?
    >


    I just needed to use this a few minutes ago, in a class declaration ...

    class ParticleDistributionBehaviorServer \
    (Microphysics__POA.ParticleDistributionBehavior):

    I had to split the line to fit within 80 columns, and without the '\'
    character I get the following error:

    =====
    $ python scoping_server.py --POA
    File "scoping_server.py", line 8
    class ParticleDistributionBehaviorServer
    ^
    SyntaxError: invalid syntax
    =====

    So the fact that you don't need it in the case of incomplete expressions
    eliminates *most* of the need for it, but there are still a few cases
    where it is required.

    Regards,
    Derek.
     
    Derek Thomson, Apr 7, 2004
    #2
    1. Advertising

  3. Russell Wallace

    John Roth Guest

    "Derek Thomson" <> wrote in message
    news:...
    > Russell Wallace wrote:
    > > Hi all,
    > >
    > > Python lets you continue a single logical line across more than one
    > > physical line, either by putting a \ at the end or letting it happen
    > > automatically with an incomplete infix operator.
    > >
    > > I'm wondering how often is this feature needed? Would there be any
    > > problems if it weren't part of the language?
    > >

    >
    > I just needed to use this a few minutes ago, in a class declaration ...
    >
    > class ParticleDistributionBehaviorServer \
    > (Microphysics__POA.ParticleDistributionBehavior):
    >
    > I had to split the line to fit within 80 columns, and without the '\'
    > character I get the following error:
    >
    > =====
    > $ python scoping_server.py --POA
    > File "scoping_server.py", line 8
    > class ParticleDistributionBehaviorServer
    > ^
    > SyntaxError: invalid syntax
    > =====
    >
    > So the fact that you don't need it in the case of incomplete expressions
    > eliminates *most* of the need for it, but there are still a few cases
    > where it is required.


    Technically, you could have split it at the period, but that
    might be even worse in terms of readability.

    BTW - why did it have to fit in 80 columns?

    John Roth
    >
    > Regards,
    > Derek.
     
    John Roth, Apr 7, 2004
    #3
  4. Russell Wallace

    Mark Jackson Guest

    Derek Thomson <> writes:
    > Russell Wallace wrote:
    > > Hi all,
    > >
    > > Python lets you continue a single logical line across more than one
    > > physical line, either by putting a \ at the end or letting it happen
    > > automatically with an incomplete infix operator.
    > >
    > > I'm wondering how often is this feature needed? Would there be any
    > > problems if it weren't part of the language?
    > >

    >
    > I just needed to use this a few minutes ago, in a class declaration ...
    >
    > class ParticleDistributionBehaviorServer \
    > (Microphysics__POA.ParticleDistributionBehavior):
    >
    > I had to split the line to fit within 80 columns, and without the '\'
    > character I get the following error:
    >
    > =====
    > $ python scoping_server.py --POA
    > File "scoping_server.py", line 8
    > class ParticleDistributionBehaviorServer
    > ^
    > SyntaxError: invalid syntax
    > =====


    This ought to work:

    class ParticleDistributionBehaviorServer(
    Microphysics__POA.ParticleDistributionBehavior):

    --
    Mark Jackson - http://www.alumni.caltech.edu/~mjackson
    Hate to interrupt with a spelling flame, but it's "Cheney,"
    not "Chaney." It may only be one letter, but it's 998 faces.
    - Mike Peterson
     
    Mark Jackson, Apr 7, 2004
    #4
  5. John Roth wrote:

    >Derek Thomson wrote:
    >>
    >>class ParticleDistributionBehaviorServer \
    >> (Microphysics__POA.ParticleDistributionBehavior):
    >>
    >> [snip]
    >>
    >>So the fact that you don't need it in the case of incomplete expressions
    >>eliminates *most* of the need for it, but there are still a few cases
    >>where it is required.

    >
    >
    > Technically, you could have split it at the period, but that
    > might be even worse in terms of readability.


    I didn't know that, but yes, it would be ugly.

    >
    > BTW - why did it have to fit in 80 columns?


    Because that's our coding standard. But it wouldn't matter if it was 120
    columns, or in fact any number you specify - I can still construct an
    example that won't work.

    Regards,
    Derek.
     
    Derek Thomson, Apr 7, 2004
    #5
  6. Mark Jackson wrote:
    >
    > This ought to work:
    >
    > class ParticleDistributionBehaviorServer(
    > Microphysics__POA.ParticleDistributionBehavior):
    >


    In fact it does work - I should have thought of that. I've just checked
    in the change to CVS. Thanks!

    Now I'll have to think of another case ... I'm sure there was another ...

    Regards,
    Derek.
     
    Derek Thomson, Apr 7, 2004
    #6
  7. Russell Wallace

    Peter Maas Guest

    Russell Wallace wrote:
    > Python lets you continue a single logical line across more than one
    > physical line, either by putting a \ at the end or letting it happen
    > automatically with an incomplete infix operator.
    >
    > I'm wondering how often is this feature needed? Would there be any
    > problems if it weren't part of the language?


    Just had this example:

    d = { 'key1' : 'A very very long string of several hundred '\
    'characters. I could use a multi-line string but '\
    'then either left white space would be part of the '\
    'string or I would have to align it at column 0 '\
    '(ugly). I could embed it into a pair of brackets '\
    'but I see no advantage over \\. Finally I could '\
    'put this string on a single line (also ugly and hard '\
    'to read).'
    ...
    }


    Mit freundlichen Gruessen,

    Peter Maas

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
    Tel +49-241-93878-0 Fax +49-241-93878-20 eMail
    -------------------------------------------------------------------
     
    Peter Maas, Apr 7, 2004
    #7
  8. Russell Wallace

    Peter Otten Guest

    Peter Maas wrote:

    > Russell Wallace wrote:
    >> Python lets you continue a single logical line across more than one
    >> physical line, either by putting a \ at the end or letting it happen
    >> automatically with an incomplete infix operator.
    >>
    >> I'm wondering how often is this feature needed? Would there be any
    >> problems if it weren't part of the language?

    >
    > Just had this example:
    >
    > d = { 'key1' : 'A very very long string of several hundred '\
    > 'characters. I could use a multi-line string but '\
    > 'then either left white space would be part of the '\
    > 'string or I would have to align it at column 0 '\
    > '(ugly). I could embed it into a pair of brackets '\
    > 'but I see no advantage over \\. Finally I could '\
    > 'put this string on a single line (also ugly and hard '\
    > 'to read).'
    > ...
    > }


    >>> d = {1: "Hi Peter, "

    .... "what do you "
    .... "think of this?"}

    Peter
     
    Peter Otten, Apr 7, 2004
    #8
  9. Russell Wallace

    David Bolen Guest

    Peter Maas <> writes:

    > Just had this example:
    >
    > d = { 'key1' : 'A very very long string of several hundred '\
    > 'characters. I could use a multi-line string but '\
    > 'then either left white space would be part of the '\
    > 'string or I would have to align it at column 0 '\
    > '(ugly). I could embed it into a pair of brackets '\
    > 'but I see no advantage over \\. Finally I could '\
    > 'put this string on a single line (also ugly and hard '\
    > 'to read).'
    > ...
    > }


    Actually that's unnecessary since you are already in a braced
    expression from the dictionary constructor. So the lines are
    implicitly continuation lines until you close the dictionary, and the
    compilers combination of adjacent string constants still works. You
    can drop the continuation characters and it'll give the same
    dictionary.

    I tend to construct parenthetical expressions if I can to avoid the
    explicit line continuation character - mostly since I think it looks
    nicer, but there are some cases where I prefer the contination.
    There's normally an alternative to the continuation, but one that I
    don't consider as readable.

    For example, one place where I've used line continuations was in
    explicit from imports, such as:

    from some.package.exceptions import ExceptionName, OtherName, \
    AnotherName, AndAnotherName

    where I find this more readable than repeating the "from <package>
    import" portion of the line multiple times.

    I also use it to suppress leading newlines in triple quoted strings,
    while permitting me to write the entire string at the same indent
    level, e.g.:

    MSG = """\
    blah blah blah

    and some more blah blah blah
    """

    I'll also use it sometimes for string formatting operations (either
    standalone or as part of print) where sometimes I think it looks nicer
    than putting the entire expression in parenthesis), e.g.:

    some_variable = 'This is a formatting string args %s %s %s' % \
    (argument1, argument2, argument3)


    -- David
     
    David Bolen, Apr 7, 2004
    #9
  10. On Wed, 07 Apr 2004 14:05:29 GMT, (Russell
    Wallace) wrote:

    >Hi all,
    >
    >Python lets you continue a single logical line across more than one
    >physical line, either by putting a \ at the end or letting it happen
    >automatically with an incomplete infix operator.
    >
    >I'm wondering how often is this feature needed? Would there be any
    >problems if it weren't part of the language?


    I don't have any ready examples, but I do occasionally need it, if
    only for readability porpoises, or more often to fit a line to a
    certain length limit. If a parenthetical expression will work, and
    doesn't look stranger (really can't think of a case), I'll use that.

    But problems there'll surely be - with legacy code compatibility.


    >--
    >"Sore wa himitsu desu."

    Not really it isn't ;-)

    --
    Christopher
     
    Christopher Koppler, Apr 7, 2004
    #10
  11. On Wed, 07 Apr 2004 17:53:00 GMT, Christopher Koppler
    <> wrote:

    >On Wed, 07 Apr 2004 14:05:29 GMT, (Russell
    >Wallace) wrote:
    >
    >>Python lets you continue a single logical line across more than one
    >>physical line, either by putting a \ at the end or letting it happen
    >>automatically with an incomplete infix operator.

    >
    >I don't have any ready examples, but I do occasionally need it, if
    >only for readability porpoises, or more often to fit a line to a
    >certain length limit. If a parenthetical expression will work, and
    >doesn't look stranger (really can't think of a case), I'll use that.


    Okay, thanks.

    >But problems there'll surely be - with legacy code compatibility.


    *nod* I was more asking whether it was a good idea to include it
    originally than whether it could be removed now, though I suppose
    since 3.0 is supposed to be breaking with backward compatibility,
    there's the question of whether it should be retained there.

    >Not really it isn't ;-)


    ^.^

    --
    "Sore wa himitsu desu."
    To reply by email, remove
    the small snack from address.
    http://www.esatclear.ie/~rwallace
     
    Russell Wallace, Apr 7, 2004
    #11
  12. On Wed, 07 Apr 2004 14:05:29 GMT,
    (Russell Wallace) wrote:

    > Python lets you continue a single logical line across more than
    > one physical line, either by putting a \ at the end or letting
    > it happen automatically with an incomplete infix operator.


    > I'm wondering how often is this feature needed? Would there be
    > any problems if it weren't part of the language?


    Completely contrived:

    if a == b or b == c or c == d or d == e or e == f or f == g ....

    No, I can't imagine this sort of thing actually coming up, and
    yes, it's "fixable" with some extra parenthesese:

    if (a == b or b == c or c == d) or (d == e or e == f
    or f == g ....

    Regards,
    Heather

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli
     
    Heather Coppersmith, Apr 8, 2004
    #12
  13. Russell Wallace

    Jeff Epler Guest

    I would not miss \-continuations much.

    I think you're mistaken about continuations "automatically with an
    incomplete infix operator":
    >>> 1 +

    File "<stdin>", line 1
    1 +
    ^
    Did I misunderstand what you meant?

    I would sorely miss lines continued due to the presence of nesting
    expressions, like
    d = {
    'x': 1,
    'y': 2
    }
    and
    if (x == 3
    and y == x * 4):
    return True
    These kinds of continued lines are IMO easier to get right than
    \-continuations, because those rely on absence of whitespace in a place
    where the presence rarely makes a visible difference (backslash space
    newline is not a continuation).

    Jeff
     
    Jeff Epler, Apr 8, 2004
    #13
  14. Russell Wallace

    Peter Maas Guest

    Peter Otten wrote:
    >>d = { 'key1' : 'A very very long string of several hundred '\
    >> 'characters. I could use a multi-line string but '\

    [...]
    >>>>d = {1: "Hi Peter, "

    >
    > ... "what do you "
    > ... "think of this?"}


    You got me :) I know implicit line continuation but thought that
    it applies only to cases where the strings are direct elements of
    the bracket like in ["s1", "s2"]. OK, then my example should read

    s = 'A very very long string of several hundred '\
    'characters. I could use a multi-line string but '\
    [...]

    If there are more than three lines you will type less with a
    pair of brackets.

    Mit freundlichen Gruessen,

    Peter Maas

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
    Tel +49-241-93878-0 Fax +49-241-93878-20 eMail
    -------------------------------------------------------------------
     
    Peter Maas, Apr 8, 2004
    #14
    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. Batista, Facundo

    RE: Are line continuations needed?

    Batista, Facundo, Apr 7, 2004, in forum: Python
    Replies:
    9
    Views:
    335
    Duncan Booth
    Apr 9, 2004
  2. Mike Thompson
    Replies:
    17
    Views:
    757
  3. Robert
    Replies:
    0
    Views:
    515
    Robert
    Feb 12, 2006
  4. Florian Lindner

    RFC 822 continuations

    Florian Lindner, May 2, 2006, in forum: Python
    Replies:
    2
    Views:
    290
  5. Gerald Britton
    Replies:
    1
    Views:
    244
    Carl Banks
    Jan 21, 2011
Loading...

Share This Page