Reduce need of backslash

Discussion in 'Python' started by Nicolas Fleury, Sep 26, 2003.

  1. Hi,

    I was wondering if the need for \ could be reduce in the language. For
    example, could a line ending with = or + could be automaticly considered
    incomplete?

    Regards,

    Nicolas
    Nicolas Fleury, Sep 26, 2003
    #1
    1. Advertising

  2. Nicolas Fleury

    Peter Otten Guest

    Nicolas Fleury wrote:

    > I was wondering if the need for \ could be reduce in the language. For
    > example, could a line ending with = or + could be automaticly considered
    > incomplete?


    Did you know about (...)?

    >>> ("alpha"

    .... + "beta"
    .... + "gamma"
    ....
    .... )
    'alphabetagamma'
    >>>



    Peter
    Peter Otten, Sep 26, 2003
    #2
    1. Advertising

  3. Nicolas Fleury

    Peter Hansen Guest

    Nicolas Fleury wrote:
    >
    > I was wondering if the need for \ could be reduce in the language. For
    > example, could a line ending with = or + could be automaticly considered
    > incomplete?


    What should Python do in the following case?

    a = 5 +
    someFunc(a)

    Okay, you want it to quietly add 5 and the result of someFunc() together and
    assign to "a". What if I told you that I actually had intended to add 5
    plus "b" and assign to a, then call someFunc() and discard the return value.

    You've just turned a nice clean error message from the compiler into a
    silent and possibly deadly bug.

    "Explicit is better than implicit, and errors should never pass silently"
    as has been noted again recently in another thread.

    -Peter
    Peter Hansen, Sep 26, 2003
    #3
  4. Nicolas Fleury

    Duncan Booth Guest

    Nicolas Fleury <_remove_the_> wrote in
    news:h4Ycb.75236$:

    > I was wondering if the need for \ could be reduce in the language. For
    > example, could a line ending with = or + could be automaticly considered
    > incomplete?


    You very rarely need \ as it is. Any parenthesised expression may be split
    across lines without problems.

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
    Duncan Booth, Sep 26, 2003
    #4
  5. Nicolas Fleury

    John Roth Guest

    "Duncan Booth" <> wrote in message
    news:Xns9402A53AD8C02duncanrcpcouk@127.0.0.1...
    > Nicolas Fleury <_remove_the_> wrote in
    > news:h4Ycb.75236$:
    >
    > > I was wondering if the need for \ could be reduce in the language. For
    > > example, could a line ending with = or + could be automaticly considered
    > > incomplete?

    >
    > You very rarely need \ as it is. Any parenthesised expression may be split
    > across lines without problems.


    And you can parenthesize a surprisingly large number of places. One
    that I learned just recently was that the entire operand string of the
    print statement can be put into parenthesis.

    John Roth
    >
    > --
    > Duncan Booth
    > int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    > "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
    John Roth, Sep 26, 2003
    #5
  6. John Roth wrote:

    > "Duncan Booth" <> wrote in message
    > And you can parenthesize a surprisingly large number of places. One
    > that I learned just recently was that the entire operand string of the
    > print statement can be put into parenthesis.


    I'm not sure what you mean exactly.
    print (1,2) prints a tuple while print 1,2 prints a different thing.

    Regards,

    Nicolas
    Nicolas Fleury, Sep 26, 2003
    #6
  7. Nicolas Fleury wrote:
    > John Roth wrote:
    >
    >> "Duncan Booth" <> wrote in message
    >> And you can parenthesize a surprisingly large number of places. One
    >> that I learned just recently was that the entire operand string of the
    >> print statement can be put into parenthesis.

    >
    >
    > I'm not sure what you mean exactly.
    > print (1,2) prints a tuple while print 1,2 prints a different thing.


    Ok, print (1) doesn't print a tuple... forget my question...

    Nicolas
    Nicolas Fleury, Sep 26, 2003
    #7
  8. Peter Otten wrote:

    > Nicolas Fleury wrote:
    >
    >
    >>I was wondering if the need for \ could be reduce in the language. For
    >>example, could a line ending with = or + could be automaticly considered
    >>incomplete?

    >
    >
    > Did you know about (...)?


    Actually, no. But it's still not what I'm looking for. It's just that
    I'm used to languages where I can put my code on multiple easily to make
    lines shorter. As John pointed, it's possible to add () to print. I
    just discovered that it can be done with return also. I wonder how to
    remove the need for \ in that example:

    parser.StartElementHandler = \
    lambda name, attrs: \
    GenericParser.handleElementStart(self, name, attrs)

    Regards,

    Nicolas
    Nicolas Fleury, Sep 26, 2003
    #8
  9. Peter Hansen wrote:
    > What should Python do in the following case?
    >
    > a = 5 +
    > someFunc(a)
    >
    > Okay, you want it to quietly add 5 and the result of someFunc() together and
    > assign to "a". What if I told you that I actually had intended to add 5
    > plus "b" and assign to a, then call someFunc() and discard the return value.
    >
    > You've just turned a nice clean error message from the compiler into a
    > silent and possibly deadly bug.


    What if the second line would be indented? Indentation is already used
    to determine blocks, why not instructions? For example, the following
    is not error-prone at all:

    a = 5 +
    someFunc()

    Doesn't it stay with python minimalistic philosophy?

    Regards,

    Nicolas
    Nicolas Fleury, Sep 26, 2003
    #9
  10. Nicolas Fleury

    John Roth Guest

    "Nicolas Fleury" <_remove_the_> wrote in message
    news:MI_cb.75294$...
    > Peter Otten wrote:
    >
    > > Nicolas Fleury wrote:
    > >
    > >
    > >>I was wondering if the need for \ could be reduce in the language. For
    > >>example, could a line ending with = or + could be automaticly considered
    > >>incomplete?

    > >
    > >
    > > Did you know about (...)?

    >
    > Actually, no. But it's still not what I'm looking for. It's just that
    > I'm used to languages where I can put my code on multiple easily to make
    > lines shorter. As John pointed, it's possible to add () to print. I
    > just discovered that it can be done with return also. I wonder how to
    > remove the need for \ in that example:
    >
    > parser.StartElementHandler = \
    > lambda name, attrs: \
    > GenericParser.handleElementStart(self, name, attrs)


    How about:

    parser.StartElementHandler = (lambda name, attrs:
    GenericParser.handleElementStart(self, name, attrs))

    John Roth
    >
    > Regards,
    >
    > Nicolas
    >
    John Roth, Sep 26, 2003
    #10
  11. On Fri, 26 Sep 2003 13:34:22 -0400, Nicolas Fleury
    <_remove_the_> wrote:

    >What if the second line would be indented? Indentation is already used
    >to determine blocks, why not instructions? For example, the following
    >is not error-prone at all:
    >
    >a = 5 +
    > someFunc()
    >
    >Doesn't it stay with python minimalistic philosophy?


    I don't know the original rationale, but to me this mostly looks ugly.
    Maybe I'd get used to it if it happened a lot, but this is rarely an
    issue in practice.

    Basically, when I need to break an expression over multiple lines, the
    odds are that it already has parentheses anyway. And I wouldn't indent
    that way anyway, I'd do it as...

    a = ( firstitem
    + seconditem
    + ( thirditem
    * fourthitem
    )
    )

    .... ie using indentation to clarify the structure of the expression
    much as I would with block structuring - whether I was using Python or
    some other language.

    In a way I can imagine some value to putting a statement in
    parentheses. It would look very Lispy, but then some would say that's
    a good thing.

    Occasionally, I go through a phase of thinking semicolons should be
    used as compulsory statement terminators rather than as the rarely
    used separators they are now - in which case '\' would be completely
    redundant - but it really is a trivial issue, whereas the idea of
    changing such a fundamental piece of syntax - well, don't go there.


    --
    Steve Horne

    steve at ninereeds dot fsnet dot co dot uk
    Stephen Horne, Sep 26, 2003
    #11
  12. Stephen Horne wrote:
    > Basically, when I need to break an expression over multiple lines, the
    > odds are that it already has parentheses anyway. And I wouldn't indent
    > that way anyway, I'd do it as...
    >
    > a = ( firstitem
    > + seconditem
    > + ( thirditem
    > * fourthitem
    > )
    > )


    Didn't know about that possibility; I though it would assign a one
    element tuple. I just read that a one-element tuple is written (x,).
    Everything's fine now, thx ;)

    Nicolas
    Nicolas Fleury, Sep 26, 2003
    #12
  13. John Roth wrote:
    > How about:
    >
    > parser.StartElementHandler = (lambda name, attrs:
    > GenericParser.handleElementStart(self, name, attrs))
    >


    That's perfect. All my incomprehension was because I didn't know a
    one-element tuple is written (x,) and not (x). Thx

    Nicolas
    Nicolas Fleury, Sep 26, 2003
    #13
  14. On Fri, 26 Sep 2003 16:33:25 +0200, Peter Otten <> wrote:

    >Nicolas Fleury wrote:
    >
    >> I was wondering if the need for \ could be reduce in the language. For
    >> example, could a line ending with = or + could be automaticly considered
    >> incomplete?

    >
    >Did you know about (...)?
    >
    >>>> ("alpha"

    >... + "beta"
    >... + "gamma"
    >...
    >... )
    >'alphabetagamma'
    >>>>

    >

    Did you know the tokenizer concatenates white-space-separated string literals into
    a single literal? ;-)

    >>> ("alpha"

    .... "beta"
    .... "gamma"
    .... )
    'alphabetagamma'

    Note the effect in code:

    >>> import dis
    >>> f1 = lambda: ("alpha"

    ... + "beta"
    ... + "gamma"
    ... )
    >>> f2 = lambda: ("alpha"

    ... "beta"
    ... "gamma"
    ... )
    >>> dis.dis(f1)

    1 0 LOAD_CONST 1 ('alpha')
    3 LOAD_CONST 2 ('beta')
    6 BINARY_ADD
    7 LOAD_CONST 3 ('gamma')
    10 BINARY_ADD
    11 RETURN_VALUE
    >>> dis.dis(f2)

    1 0 LOAD_CONST 1 ('alphabetagamma')
    3 RETURN_VALUE

    Regards,
    Bengt Richter
    Bengt Richter, Sep 27, 2003
    #14
  15. Stephen Horne wrote:
    > Basically, when I need to break an expression over multiple lines, the
    > odds are that it already has parentheses anyway. And I wouldn't indent
    > that way anyway, I'd do it as...
    >
    > a = ( firstitem
    > + seconditem
    > + ( thirditem
    > * fourthitem
    > )
    > )


    But that's indentation anyway. If the rule would be "all lines with a
    superior indentation are part of the previous line", would that work?
    This way it would not be necessary to add the parenthesis. I wonder if
    I'm missing something...

    Regards,

    Nicolas
    Nicolas Fleury, Sep 27, 2003
    #15
  16. On Sat, 27 Sep 2003 15:49:59 -0400, Nicolas Fleury
    <_removethe_> wrote:

    >Stephen Horne wrote:
    >> Basically, when I need to break an expression over multiple lines, the
    >> odds are that it already has parentheses anyway. And I wouldn't indent
    >> that way anyway, I'd do it as...
    >>
    >> a = ( firstitem
    >> + seconditem
    >> + ( thirditem
    >> * fourthitem
    >> )
    >> )

    >
    >But that's indentation anyway. If the rule would be "all lines with a
    >superior indentation are part of the previous line", would that work?


    Probably yes, but it has been discussed before and never generated a
    clear enough choice or sufficient will to make the change.

    My personal preference would be closer to Haskells offside rule,
    allowing me to write something similar to my example above but with
    indentation reducing the need for parentheses. But it really isn't an
    important issue IMO.


    --
    Steve Horne

    steve at ninereeds dot fsnet dot co dot uk
    Stephen Horne, Sep 27, 2003
    #16
  17. Nicolas Fleury

    Peter Otten Guest

    Bengt Richter wrote:

    > Did you know the tokenizer concatenates white-space-separated string
    > literals into a single literal? ;-)
    >
    >>>> ("alpha"

    > ... "beta"
    > ... "gamma"
    > ... )
    > 'alphabetagamma'
    >
    > Note the effect in code:
    >
    > >>> import dis
    > >>> f1 = lambda: ("alpha"

    > ... + "beta"
    > ... + "gamma"
    > ... )
    > >>> f2 = lambda: ("alpha"

    > ... "beta"
    > ... "gamma"
    > ... )
    > >>> dis.dis(f1)

    > 1 0 LOAD_CONST 1 ('alpha')
    > 3 LOAD_CONST 2 ('beta')
    > 6 BINARY_ADD
    > 7 LOAD_CONST 3 ('gamma')
    > 10 BINARY_ADD
    > 11 RETURN_VALUE
    > >>> dis.dis(f2)

    > 1 0 LOAD_CONST 1 ('alphabetagamma')
    > 3 RETURN_VALUE


    No. Or I would have chosen another example :)

    But now you point it out I see it's used quite frequently in the library.

    Peter
    Peter Otten, Sep 29, 2003
    #17
    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. Prabh
    Replies:
    2
    Views:
    488
    Chris Smith
    May 13, 2004
  2. Razvan

    slash vs backslash

    Razvan, Jul 7, 2004, in forum: Java
    Replies:
    7
    Views:
    17,426
    Razvan
    Jul 8, 2004
  3. Skinny Guy
    Replies:
    1
    Views:
    1,622
  4. Dennis Marks

    Slash and Backslash

    Dennis Marks, May 16, 2004, in forum: HTML
    Replies:
    5
    Views:
    14,190
    Hywel
    May 16, 2004
  5. Vincent Texier
    Replies:
    3
    Views:
    2,243
    Vincent Texier
    Jul 1, 2004
Loading...

Share This Page