Multiline code - trailing slash usage

A

abcd

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?
 
S

Steve Holden

abcd said:
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
 
C

Christoph Haas

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
 
L

Larry Bates

abcd said:
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
 
S

Steven Bethard

abcd said:
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
 
B

Bruno Desthuilliers

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...
 
D

Duncan Booth

Steve Holden said:
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!!!!")
 
B

Ben Finney

abcd said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top