473,573 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to write comments after escaping newline

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" + \
someothervariab le

print mystring

The above works , but if I add comments

mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariab le #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
3 1609
p brian wrote:
mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariab le #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
someOtherVariab le) # 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 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" + \
someothervariab le
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
someothervariab le #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, someothervariab le)

--
Yours,

Andrei

=====
Real contact info (decode with rot13):
ce******@jnanqb b.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 brian wrote:
mystring = "hello " + someVariable + \
"multiple lines 4 clarity" + \
someothervariab le

The above works , but if I add comments

mystring = "hello " + someVariable + \ #comment
"multiple lines 4 clarity" + \ #comment
someothervariab le #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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
437
by: ChrisC | last post by:
if I had outfile.write((char*) record, sizeof( record )) how would I get write to return a newline? as it is it doesn't. -- ChrisC - chrispche@nospam.yahoo.co.uk
4
2943
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400, like DOS, uses memory-mapped display, two bytes per character (one char byte and one attribute byte). We can get the screen offset allright, and...
9
1398
by: Hendrik Wendler | last post by:
Hi everybody, this may be a stupid question: i want to strip comments from a .cpp file. cpp comments look like: // (two slashes) .... comment until newline -->|
4
2657
by: Jeff Rodriguez | last post by:
Main just loops over this while it's not null. The segfault occurs at this line: *line = (char)ch; Also, please don't just fix the code. I would like to know why exactly this isn't working so I can avoid problems with it in the future. If there are any references I should check out let me know. Full highlighted code at:
20
1436
by: drM | last post by:
Could anyone please offer some advice: Trying to play with character arrays in C, the code below generates this error. Anyone see something wrong? thank you in advance. >>>>>>>> #include <stdio.h>
11
3089
by: Michael Powe | last post by:
How can I make an XHTML-compliant form of an expression in this format: document.write("<scr"+"ipt type='text/javascript' src='path/to/file.js'>"+"</scr"+"ipt>"); this turns out to be a non-trivial exercise. inserting '&lt;' and '&gt;' causes the browser to write the text to the page as literal text rather than as the intended script element. ...
1
1237
by: George Sakkis | last post by:
The tokenize.generate_tokens function seems to handle in a context- sensitive manner the new line after a comment: .... # hello world .... x = ( .... # hello world .... ) .... ''' .... print repr(t) ....
7
4103
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour doesn't occur with non-control characters. For example, if this program (called test.py): from optparse import OptionParser parser =...
6
1933
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a non- comment Python statement, there is no error. Here's an example (using 2.5.1 on OS X) .... def foo(): .... print 'bar' """ <code object...
0
7778
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8017
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5290
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3731
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2203
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1300
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1039
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.