Multiline code - trailing slash usage

Discussion in 'Python' started by abcd, Mar 15, 2007.

  1. abcd

    abcd Guest

    When do I need to use a trailing slash to separate code over multiple
    lines.

    For example:

    x = "hello world, this is my multiline " + \
    "string!!!!"

    x = {'name' : \
    'bob'}

    Do I need to use the "\" in the above examples? When do i need to use
    it?
    abcd, Mar 15, 2007
    #1
    1. Advertising

  2. abcd

    Steve Holden Guest

    abcd wrote:
    > When do I need to use a trailing slash to separate code over multiple
    > lines.
    >
    > For example:
    >
    > x = "hello world, this is my multiline " + \
    > "string!!!!"
    >
    > x = {'name' : \
    > 'bob'}
    >
    > Do I need to use the "\" in the above examples? When do i need to use
    > it?
    >

    It's only needed if the end of the line could also be the end of the
    statement. So if there's an unclosed parenthesis, bracket or brace you
    can move to the next line without using a continuation backslash.

    So it's needed in the first example, but not in the second.

    Note also, by the way, that the Python interpreter will concatenate two
    adjacent string literals, so you could also have written

    x = "hello world, this is my multiline " \
    "string!!!!"

    and this would have saved you a run-time string concatenation :)

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    Blog of Note: http://holdenweb.blogspot.com
    See you at PyCon? http://us.pycon.org/TX2007
    Steve Holden, Mar 15, 2007
    #2
    1. Advertising

  3. On Thursday 15 March 2007 15:57, abcd wrote:
    > When do I need to use a trailing slash to separate code over multiple
    > lines.
    >
    > For example:
    >
    > x = "hello world, this is my multiline " + \
    > "string!!!!"


    Needed. Although you can omit the "+".

    > x = {'name' : \
    > 'bob'}


    Not needed because you are inside the curly brackets {} and it's clear
    where the statement ends.

    > Do I need to use the "\" in the above examples? When do i need to use
    > it?


    Cheers
    Christoph
    Christoph Haas, Mar 15, 2007
    #3
  4. abcd

    Larry Bates Guest

    abcd wrote:
    > When do I need to use a trailing slash to separate code over multiple
    > lines.
    >
    > For example:
    >
    > x = "hello world, this is my multiline " + \
    > "string!!!!"
    >
    > x = {'name' : \
    > 'bob'}
    >
    > Do I need to use the "\" in the above examples? When do i need to use
    > it?
    >

    You need to use it when your are not inside some context that makes it
    clear to Python that there's more to the line:

    You don't need it here because python knows you are inside a list (same is
    true for tuple).

    a=[1,
    2,
    3
    ]

    Same for a dictionary:

    a={'a1': 1,
    'a2': 2,
    'a3': 3
    }

    Also when you are inside call list of a function

    a=foo(a,"this is a very long string",
    arg3, arg4,
    kwarg1='one', kwarg2='two')

    Python knows you aren't done because you haven't provided the closing
    parenthesis.

    I do this in list comprehensions also:

    n=[(variable1, variable2) for variable1, variable2 in something
    if variable1.startswith('z')]

    You do need it in your first example, but not in your second.

    -Larry
    Larry Bates, Mar 15, 2007
    #4
  5. abcd wrote:
    > When do I need to use a trailing slash to separate code over multiple
    > lines.
    >
    > For example:
    >
    > x = "hello world, this is my multiline " + \
    > "string!!!!"


    Yes.

    >
    > x = {'name' : \
    > 'bob'}


    No.

    You don't need trailing slashes whenever there's a pair of {}, [] or ()
    wrapping things.

    I never use trailing slashes -- I just wrap the expression in parentheses.

    STeVe
    Steven Bethard, Mar 15, 2007
    #5
  6. abcd a écrit :
    > When do I need to use a trailing slash to separate code over multiple
    > lines.
    >
    > For example:
    >
    > x = "hello world, this is my multiline " + \
    > "string!!!!"


    Here you don't need the +

    > x = {'name' : \
    > 'bob'}


    And here you don't need the antislash

    > Do I need to use the "\" in the above examples? When do i need to use
    > it?
    >


    IIRC, lists, tuples and dicts litterals, function args, list comps and
    generator expressions can span multiple lines. In any other case, you
    need the antislash. But you'd better check in the FineManual...
    Bruno Desthuilliers, Mar 15, 2007
    #6
  7. abcd

    Duncan Booth Guest

    Steve Holden <> wrote:

    > x = "hello world, this is my multiline " \
    > "string!!!!"
    >
    > and this would have saved you a run-time string concatenation :)


    or use parentheses for an alternative which doesn't need the backslash:

    x = ("hello world, this is my multiline "
    "string!!!!")
    Duncan Booth, Mar 15, 2007
    #7
  8. abcd

    Ben Finney Guest

    "abcd" <> writes:

    > When do I need to use a trailing slash to separate code over multiple
    > lines.
    >
    > For example:
    >
    > x = "hello world, this is my multiline " + \
    > "string!!!!"


    You can either do that, or you can use parentheses:

    x = ( "foo" +
    "bar" )

    Note that you can make this read better *and* be faster, because
    Python's parser will concatenate adjacent string values into a single
    string value before compilation:

    x = ( "foo"
    "bar" )

    Both these result in x being bound to the string value "foobar". The
    second example doesn't even involve a concatenation operation at
    run-time.

    > x = {'name' : \
    > 'bob'}


    Python allows parentheses '()', brackets '[]' and braces '{}' to
    enclose multi-line statements.

    x = { 'name':
    "bob" }

    > Do I need to use the "\" in the above examples? When do i need to
    > use it?


    I almost never use it to extend a statement; only sometimes within a
    triple-quoted string. Parentheses can be used just about anywhere you
    might otherwise need backslash-escaped line breaks.

    --
    \ "My, your, his, hers, ours, theirs, its. |
    `\ I'm, you're, he's, she's, we're, they're, it's." |
    _o__) -- Anonymous, alt.sysadmin.recovery |
    Ben Finney
    Ben Finney, Mar 15, 2007
    #8
    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. Replies:
    1
    Views:
    305
    Max Erickson
    Nov 10, 2006
  2. Harlan Messinger

    ASP.NET inserts trailing slash in tag

    Harlan Messinger, Feb 18, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    466
    Harlan Messinger
    Feb 19, 2008
  3. Jeff Mitchell

    trailing slash issue in Find.find

    Jeff Mitchell, Aug 24, 2003, in forum: Ruby
    Replies:
    0
    Views:
    101
    Jeff Mitchell
    Aug 24, 2003
  4. Stan Brown
    Replies:
    6
    Views:
    366
    Stan Brown
    Oct 29, 2003
  5. Sebastian Bauer

    HTTP::Request, trailing slash

    Sebastian Bauer, Jun 29, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    172
    Sebastian Bauer
    Jul 1, 2004
Loading...

Share This Page