Trying to write comments after escaping newline

Andrei project5 at redrival.net
Tue Apr 27 14:47:42 EDT 2004


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):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.



More information about the Python-list mailing list