Comments within a line

E

Edward Diener

Has there ever been any discussion of adding comments notation to Python,
such as C++'s /* */, which could occur in the middle of a line or span
multiple lines ? I would like to see it as a more elegant solution for
quickly commenting out code. I am aware of the """ """ technique around
multiple lines but even that seems a poor hack. A comment that could enclose
any sequence of characters would be most welcome.
 
H

Heike C. Zimmerer

Edward Diener said:
Has there ever been any discussion of adding comments notation to Python,
such as C++'s /* */, which could occur in the middle of a line or span
multiple lines ? I would like to see it as a more elegant solution for
quickly commenting out code.

In C/C++, you wouldn't use /* */ for commenting out code, since it
would collide with any comment already there (comments can't be
nested). Instead, the usual way is using #if 0 ... #endif.

You can use "if 0:" with python as well, but of course, you'd have to
indent the entire commented-out block. With most editors, this isn't
much of a problem. I usually use some smaller, "odd" indent than
usual to have an indicication later where the block ends.

- Heike
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[Heike C. Zimmerer]
You can use "if 0:" with python as well, but of course, you'd have
to indent the entire commented-out block. With most editors, this
isn't much of a problem. I usually use some smaller, "odd" indent
than usual to have an indicication later where the block ends.

One common trick is to embed a region to comment within triple quotes
(for example, you write ''' (three single quotes) at the beginning of
the text on the first line of your block, and the same after the end of
the text on the last line of your block. I've even read that strings as
statements are optimised out (if not in doc-string position).

I do not use this trick myself. With any reasonable editor, it is very
easy to comment or uncomment a bunch of lines in a simple operation, and
this is neater.
 
E

Edward Diener

Heike said:
In C/C++, you wouldn't use /* */ for commenting out code, since it
would collide with any comment already there (comments can't be
nested).

That's true but many times it will not, especially if one comments out code
within a line, or if one is using // liberally also.
Instead, the usual way is using #if 0 ... #endif.

Yes, this is the usual method in C++ which I often use, but Python has no
preprocessor.
You can use "if 0:" with python as well, but of course, you'd have to
indent the entire commented-out block. With most editors, this isn't
much of a problem. I usually use some smaller, "odd" indent than
usual to have an indicication later where the block ends.

Decent idea. Still some sort of multi-line comment notation in Python would
be preferable.
 
D

Dan Bishop

Edward Diener said:
That's true but many times it will not, especially if one comments out code
within a line, or if one is using // liberally also.

I once saw someone write C code that commented out code with /* */
comments, and then decided he needed to temporarily use that code
again, so he commented out the /* and */ with //'s.
 

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
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top