Group comment

K

ketulp_baroda

Hi
Can I comment a group of statements together i.e
If I a 4 statements say:
i=1
i+=1
print i
print i+1

Instead of commenting each satement iindividually by # is there
something which allws me to comment these 4 statements in one go as in
C++ using /*...*/
 
S

Skip Montanaro

ketulp> Can I comment a group of statements together i.e
ketulp> If I a 4 statements say:
ketulp> i=1
ketulp> i+=1
ketulp> print i
ketulp> print i+1

ketulp> Instead of commenting each satement iindividually by # is there
ketulp> something which allws me to comment these 4 statements in one go
ketulp> as in C++ using /*...*/

Many people use triple-quoted strings for this:

"""
i=1
i+=1
print i
print i+1
"""

It's not a comment, strictly speaking, however it generally achieves the
desired results. Even less comment-like is "if False:":

if False:
i=1
i+=1
print i
print i+1

That has the disadvantage that you need to reindent the lines of interest.

Skip
 
D

Diez B. Roggisch

Use (x)emacs and type

M-x comment-region

Type

C-u M-x comment-region

to uncomment. Of course you can bind that to keys. C-c-< and C-c-> will
dedent/indent the current selection. Very useful.
 
Z

ziaran

Skip said:
ketulp> Can I comment a group of statements together i.e
ketulp> If I a 4 statements say:
ketulp> i=1
ketulp> i+=1
ketulp> print i
ketulp> print i+1

ketulp> Instead of commenting each satement iindividually by # is there
ketulp> something which allws me to comment these 4 statements in one go
ketulp> as in C++ using /*...*/

Many people use triple-quoted strings for this:

"""
i=1
i+=1
print i
print i+1
"""

It's not a comment, strictly speaking, however it generally achieves the
desired results. Even less comment-like is "if False:":

if False:
i=1
i+=1
print i
print i+1

That has the disadvantage that you need to reindent the lines of interest.

Skip

And keep the """ at the same identation level of the clause!
 
C

Chema Cortes

Skip Montanaro said:
Many people use triple-quoted strings for this:

"""
i=1
i+=1
print i
print i+1
"""

It's not a comment, strictly speaking, however it generally achieves the
desired results.

But as colateral effect it will be interpreted like a doc string. It's better to
make it as a tuple:

("""
i=1
i+=1
print i
print i+1
""")

or, like a hybrid:

if 0:"""
i=1
i+=1
print i
print i+1
"""
 
D

David Bolen

Diez B. Roggisch said:
Use (x)emacs and type

M-x comment-region

Type

C-u M-x comment-region

to uncomment. Of course you can bind that to keys. C-c-< and C-c-> will
dedent/indent the current selection. Very useful.

Since presumably you're using python-mode (from your comment about the
dedent/indent bindings), you may like to know that the commenting
commands are already bound to keys by default. C-c # to comment,
C-u C-c # to uncomment.

-- David
 
B

Bengt Richter

ketulp> Can I comment a group of statements together i.e
ketulp> If I a 4 statements say:
ketulp> i=1
ketulp> i+=1
ketulp> print i
ketulp> print i+1

ketulp> Instead of commenting each satement iindividually by # is there
ketulp> something which allws me to comment these 4 statements in one go
ketulp> as in C++ using /*...*/

Many people use triple-quoted strings for this:

"""
i=1
i+=1
print i
print i+1
"""

Though there's always the corner case where there's already something triple-quoted in
the segment you want to comment/quote out.

If we had string-delimited quoting a la mime, we could solve that. E.g.,

q'arbitrary string'<this is '''quoted''' and """triple is ignored""" ...>'arbitrary string'

or
Q'arbitrary string'
<this is '''quoted''' and """triple is ignored""" ...>
'arbitrary string'

Where the upper case Q signals the syntax that the line tail after the delimiter is ignored.
(thus the second quote above also includes the \n at the end of the quoted < ...> line)


thus

Q'XXX'
i=1
i+=1
print i
print i+1
'XXX'

and then no problem to do:

Q'YYY'
Q'XXX'
i=1
i+=1
print i
print i+1
'XXX'
'YYY'

(The single quotes are for readability, not part of the actual quoting delimiters)
You could deal with the final-escape char problem if you had a no-escapes raw string
version of this. Maybe signal that with double quoted delimiter strings, e.g.,

q"+++"this ends in backslash\"+++"

which would define the same string constant as 'this ends in backslash\\'

It's not a comment, strictly speaking, however it generally achieves the
desired results. Even less comment-like is "if False:":

if False:
i=1
i+=1
print i
print i+1

That has the disadvantage that you need to reindent the lines of interest.

The other side of the coin is that it's sometimes useful to write

if True:
# .. pasted code that has indentation

"if True" is also handy to defer processing in interactive mode, e.g.,
... print 'hi'
... print 'ho'
...
hi
ho

That's my clpy quota for today ;-)

Regards,
Bengt Richter
 
J

Josiah Carlson

thus
Q'XXX'
i=1
i+=1
print i
print i+1
'XXX'

You may as well suggest /* and */, as they are far more well known, and
significantly more acceptable (in my opinion) than your syntax.
Considering that /* */ has not already been added to the language, I
doubt that an aribirary magical syntax for commenting code would be added.

- Josiah
 
B

Bengt Richter

You may as well suggest /* and */, as they are far more well known, and
significantly more acceptable (in my opinion) than your syntax.
Considering that /* */ has not already been added to the language, I
doubt that an aribirary magical syntax for commenting code would be added.

/*
/* you missed the point */
*/

Regards,
Bengt Richter
 
J

Josiah Carlson

You may as well suggest /* and */, as they are far more well known, and
/*
/* you missed the point */
*/

I understood that nuance, and while your syntax allows nesting of
comments, it is still arbitrary and magical. May as well use an "if
False:" construct and gain the benefit of visual indentation, regardless
of an editor's syntax highlighting capability.

- Josiah
 
B

Ben Finney

May as well use an "if False:" construct and gain the benefit of
visual indentation, regardless of an editor's syntax highlighting
capability.

As well as losing any possible ambiguity about whether the code will be
executed. I like it.
 

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,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top