473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Multiline code - trailing slash usage

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?

Mar 15 '07 #1
7 2942
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
Mar 15 '07 #2
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

Mar 15 '07 #3
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
Mar 15 '07 #4
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...
Mar 15 '07 #5
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
Mar 15 '07 #6
Steve Holden <st***@holdenweb.comwrote:
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!!!!")
Mar 15 '07 #7
"abcd" <co*******@gmail.comwrites:
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

Mar 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
2
by: rick_muller | last post by:
I'm trying to embed a Python interpreter in a GUI I'm developing, and I'm having trouble understanding the proper use of code.InteractiveInterpreter. Here's what I'm trying: % python Python...
1
by: nomadx | last post by:
hello, could someone please check this out? #usr/local/bin/perl # Filelist.pl version 0.1 use strict; use CGI qw(:all); use CGI::Pretty; #use CGI::Carp qw(fatalsToBrowser); use Cwd; my...
23
by: Hostile17 | last post by:
I keep coming across people, online and in real life, who believe that to code single tags like <br> and <img> with trailing slashes, <br /> and <img /> is considered "best practice" and when...
40
by: Edward Elliott | last post by:
At the risk of flogging a dead horse, I'm wondering why Python doesn't have any multiline comments. One can abuse triple-quotes for that purpose, but that's obviously not what it's for and doesn't...
1
by: metaperl.mogd | last post by:
Hi, I'm a big fan of path.py. One thing that I think is a good idea is for directories to automatically have a slash appended to them if it is not automatically added. Eg: from path import...
2
by: Tom | last post by:
I've beat my head every way I can think of against this wall. Maybe someone here just knows the answer. I'm testing an upload class on my laptop WAMP server. When I run the test script,...
11
by: Brian | last post by:
I have been working on a data reception system.. I am still finding my way around Javascript, though I am accomplishing much. I just fixed a flaw that was really hard to find. The symptoms are...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.