Group comment
Bengt Richter
bokr at oz.net
Tue Mar 2 14:45:40 EST 2004
On Mon, 1 Mar 2004 08:57:56 -0600, Skip Montanaro <skip at pobox.com> wrote:
>
> 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'
hi
>>> print 'ho'
ho
>>> if True:
... print 'hi'
... print 'ho'
...
hi
ho
That's my clpy quota for today ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list