473,799 Members | 3,017 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 1619
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
2954
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 I've written a javascript which does the math to convert the offset into row/col (i.e. left, top)...
9
1409
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
2667
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
1466
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
3113
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. Using escape codes seemed to work (makes it standard compliant) but the text is not written to...
1
1245
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
4120
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 = OptionParser() parser.add_option("-d", dest="delimiter", action="store")
6
1960
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 <moduleat 0x79608, file "", line 2>
0
9685
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6804
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2937
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.