Multiline code - trailing slash usage
Ben Finney
bignose+hates-spam at benfinney.id.au
Thu Mar 15 12:27:10 EDT 2007
"abcd" <codecraig at gmail.com> 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
More information about the Python-list
mailing list