Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Multiline code - trailing slash usage

Reply
Thread Tools

Multiline code - trailing slash usage

 
 
abcd
Guest
Posts: n/a
 
      03-15-2007
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?

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      03-15-2007
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

 
Reply With Quote
 
 
 
 
Christoph Haas
Guest
Posts: n/a
 
      03-15-2007
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
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      03-15-2007
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
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      03-15-2007
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
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-15-2007
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...
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      03-15-2007
Steve Holden <(E-Mail Removed)> 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!!!!")


 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      03-15-2007
"abcd" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET inserts trailing slash in tag Harlan Messinger ASP .Net 4 02-19-2008 01:15 PM
path.py and directory naming: trailing slash automatic? metaperl.mogd@gmail.com Python 1 11-10-2006 05:03 PM
HTTP::Request, trailing slash Sebastian Bauer Perl Misc 9 07-01-2004 06:19 PM
Finding out if a string has a trailing slash Stan Brown Perl Misc 6 10-29-2003 03:09 PM
trailing slash issue in Find.find Jeff Mitchell Ruby 0 08-23-2003 11:30 PM



Advertisments