Trying to write comments after escaping newline

P

p brian

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
 
P

Peter Hansen

p said:
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
 
A

Andrei

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):
(e-mail address removed). Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
 
D

Diez B. Roggisch

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

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

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,616
Latest member
gijoji4272

Latest Threads

Top