By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

Trying to write comments after escaping newline

P: n/a
Dear all,

Weirdly enough i do not think I have come across this before. Perhaps I do
not use enough comments....

mystring = "hello " + someVariable + \
"multiple lines 4 clarity" + \
someothervariable

print mystring

The above works , but if I add comments

mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariable #comment

it stops working because instead of escaping a newline I am now just
escaping a space or tab.
Is there some clever thing i have missed?

Thanks for any advice

paul
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
p brian wrote:
mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariable #comment

it stops working because instead of escaping a newline I am now just
escaping a space or tab.
Is there some clever thing i have missed?


Yeah, don't do that. :-) Do this instead;

mystring = ("hello " + someVariable + # comment
"multiple lines 4 clarity" + # another comment
someOtherVariable) # last comment

Better yet, consider finding a way to write your code that doesn't
require comments in the middle of strings like that. It's not
necessarily a good idea to write comments just for comments' sake.
Try going for "self-documenting" code.

Finally, you could also just use string substitution to clean
this stuff up even more:

mystring = "hello %s multiple lines not needed %s" % (someVar, someOtherVar)

That's often easier on the eyes and the fingers than all those
concatenation operators.

-Peter
Jul 18 '05 #2

P: n/a
p brian wrote on Tue, 27 Apr 2004 19:38:14 +0100:
Weirdly enough i do not think I have come across this before. Perhaps I do
not use enough comments....

mystring = "hello " + someVariable + \
"multiple lines 4 clarity" + \
someothervariable
Concatenating strings is not very efficient, you should avoid it.
The above works , but if I add comments

mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariable #comment

it stops working because instead of escaping a newline I am now just
escaping a space or tab.


'\' is not an escape char if it's not inside a string - and it isn't in
this case, since there's no quotes around it. Plus you can't escape
spaces/tabs anyway, since "\ " simply means "backslash followed by space",
which when printed shows up as '\\ '.

In this case it's a line continuation character, meaning that you're saying
"this line ends here, but pretend it continues on the next one"... and then
you try to fool the parser by not making that line end there after all.

A different way to do this could be:

mystring = "hello %s multiple lines 4 clarity %s" % \
(somevariable, someothervariable)

--
Yours,

Andrei

=====
Real contact info (decode with rot13):
ce******@jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
Jul 18 '05 #3

P: n/a
p brian wrote:
mystring = "hello " + someVariable + \
"multiple lines 4 clarity" + \
someothervariable

The above works , but if I add comments

mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariable #comment

it stops working because instead of escaping a newline I am now just
escaping a space or tab.
Is there some clever thing i have missed?


To make line continuation work, the newline at the end of an line has to be
escaped using an \ - and putting your comment there makes this exactly what
you already observed: a space or tab escape instead of the newline.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.