What's the word on using """ to comment-out?

K

kj

I think I remember, early in my learning of Python, coming across
the commandment "THOU SHALT NOT USE TRIPLE-QUOTES TO COMMENT-OUT
LINES OF CODE", or something to that effect. But now I can't find
it!

Is my memory playing me a trick?

After all, from what I've seen since then, the practice of
triple-quote-commenting (or TQC, pardon the TCA) is in fact quite
common.

Is TQC OK after all?

If not, what's the case against it?

Also, has the BDFL expressed an opinion on the subject? Alternatively,
is there any other more or less "authoritative" opinion on TQC?

TIA!

~K

P.S. I can think of at least one reason to avoid TQC: it has
basically the same problems with nesting that C's /**/ has. (Sure,
in the case of TQC one could use "the other" triple quote, but it's
not hard to see that this workaround goes only so far.) But this
reason does not seem to bother many programmers, most of whom,
after all, cut their teeth with C and /**/.

P.S.2 I'm disregarding "it's un-Pythonic" as a reason against TQC,
because it's not the kind of reason that carries much weight out
here "in the trenches", as I've discovered. But, yes, I happen to
think that TQC is un-Pythonic. After all, in my programming
environment, it takes half as many keystrokes to comment a block
of code with # than it does with """. I imagine that any decent
programming environment makes #-commenting at least as easy.
Therefore, there goes convenience as an argument in defense of TQC.
Which leaves TMTOWTDI as the only possible defense for TQC, and
TMTOWTDI is pretty un-Pythonic, no? :)
 
R

rantingrick

I think I remember, early in my learning of Python, coming across
the commandment "THOU SHALT NOT USE TRIPLE-QUOTES TO COMMENT-OUT
LINES OF CODE", or something to that effect.  But now I can't find
it!

Your going to get many opinions on this subject but my stance is --
use quotes for stings and hash chars for comments -- thats the end of
it for me ;)
 
A

Aahz

I think I remember, early in my learning of Python, coming across the
commandment "THOU SHALT NOT USE TRIPLE-QUOTES TO COMMENT-OUT LINES OF
CODE", or something to that effect. But now I can't find it!

Is my memory playing me a trick?

Possibly. I certainly do that.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer
 
C

CM

After all, from what I've seen since then, the practice of
triple-quote-commenting (or TQC, pardon the TCA) is in fact quite
common.

Is TQC OK after all?

If not, what's the case against it?

I have no sense of how approved it is, and don't have a strong opinion
on it, but I would think that some cases against it could be:

- It's used for docstrings, so when you scan code it is harder to
instantly find comment blocks or docstrings if IDE parsers color code
comments differently than docstrings. An IDE I use (Boa Constructor)
uses "# XXX [comment]" as a comment that means "add to the to-do list"
as well.

- If you want to search for comments easily, you can search for #,
which will probably only bring you to comments, whereas if you search
for quote marks, they could be used in a number of different ways in
the code.

- Adhering to coding conventions is a good thing in open source
applications.
 
S

Steven D'Aprano

I think I remember, early in my learning of Python, coming across the
commandment "THOU SHALT NOT USE TRIPLE-QUOTES TO COMMENT-OUT LINES OF
CODE", or something to that effect. But now I can't find it!

Is my memory playing me a trick?

After all, from what I've seen since then, the practice of
triple-quote-commenting (or TQC, pardon the TCA) is in fact quite
common.

Oooh, I hope not... for anything but Q&D scripting, folks should be using
source control rather than filling their source code up with vast lumps
of old dead code.

Is TQC OK after all?

Only if you're lazy and slack, and being lazy and slack is itself only
okay if you are only lazy and slack *a very little bit*. In a small
script, using test-driven development, it is acceptable to comment out
dead code for a single test run. At the end of the run, you either
reverse the commenting out, or you delete the dead code.

If not, what's the case against it?

Commenting out dead code is, itself, a BAD THING, regardless of whether
you use comments or triple-quotes.

http://www.coderenaissance.com/2008/09/quit-commenting-out-dead-code.html


Triple-quoted comments are worrying, though, because the result of them
is not specified by the language. (Other than docstrings, of course.) The
CPython compiler optimizes them away at compile time, so they have no
runtime influence at all, but other implementations may not include this
optimization and so the string gets compiled into the byte-code, created
at runtime, then immediately deleted. Ouch.
 
P

Paul Rudin

kj said:
I think I remember, early in my learning of Python, coming across
the commandment "THOU SHALT NOT USE TRIPLE-QUOTES TO COMMENT-OUT
LINES OF CODE", or something to that effect. But now I can't find
it!

No idea, but it would be nice to have some multiline comment syntax
(other than # at the beginning of each line). Particularly one that can
be nested.
 
T

Tim Chase

Paul said:
No idea, but it would be nice to have some multiline comment syntax
(other than # at the beginning of each line). Particularly one that can
be nested.

Well, there's always "if 0"/"if False", but that requires
screwing with the indentation levels. Granted, any competent
text editor will allow you to easily shift code indentation (I
know Vim does, and am pretty sure Emacs will let you do the
same...YMMV with other editors).

But yes, there have been times that a multi-line commenting that
doesn't touch your indentation would be nice, and I confess to
using triple-quotes to do that (opting for """ or ''' based on
the type of triple-quotes found in the code). However, I usually
limit this for debugging purposes and they usually get pruned or
uncommented for production code.

-tkc
 
L

Lie Ryan

I think I remember, early in my learning of Python, coming across
the commandment "THOU SHALT NOT USE TRIPLE-QUOTES TO COMMENT-OUT
LINES OF CODE", or something to that effect. But now I can't find
it!

I've never heard of it, though I can think of a few reasons why TQC
might be a bad thing. Especially if a user pydoc-ed your module and see
a bunch of meaningless code.
Is my memory playing me a trick?

After all, from what I've seen since then, the practice of
triple-quote-commenting (or TQC, pardon the TCA) is in fact quite
common.

Is TQC OK after all?

I'd say it's OK for quick and dirty code, or when you're rewriting a
significant part of the code especially in early development (or you
haven't setup a version control system since it's a damn small script).
They shouldn't be permanent though, due to docstring problem.
 
G

Grant Edwards

No idea, but it would be nice to have some multiline comment syntax
(other than # at the beginning of each line). Particularly one that can
be nested.

if 0:

Seriously, that's what I generally do: mark the block of code,
indent it 1 level, add an if 0: at the top.
 
M

Michael Rudolf

Am 25.02.2010 16:07, schrieb Grant Edwards:
if 0:

Seriously, that's what I generally do: mark the block of code,
indent it 1 level, add an if 0: at the top.

I really hate it when I see something like this in other's code.
The fact that my IDE (vim) still displays this like valid code ready to
be executed can cause extreme frustration while trying to spot a bug.

This is almost as bad as "commenting out" (parts of) a for loop by
adding a continue.

I once saw this in production code and wanted to kill the original
developer, as commenting out the parts of the code with the continue (it
was a *biiiig* for-loop) for debugging purposes actually would have
*enabled* the code below, thus rendering the whole database useless.

Lucky me that I a) had backups b) set up a sandbox and c) actually saw
this before it was too late.

Regards,
Michael
 
G

Grant Edwards

Am 25.02.2010 16:07, schrieb Grant Edwards:

I really hate it when I see something like this in other's
code.

The only time you'll see that in my code is if you're watching
over my shoulder as I troublshoot something.
The fact that my IDE (vim) still displays this like valid code
ready to be executed can cause extreme frustration while
trying to spot a bug.

Nobody in their right mind _leaves_ "commented out" code like
that (or other commenting mechanisms) in a program after
they're done with whatever little experiment they were
performing.

I know people who will re-write a block of code and leave the
old code there, but comment it out, along with the date and
their name, and other such nonsense. I hate that. Keeping
track of what _used_ to be there and who changed what when is
the job of the version control system. Trying to keep the
edit-history of a file in-line as comments just makes the code
hard to read and maintain.
This is almost as bad as "commenting out" (parts of) a for
loop by adding a continue.

IMO, any sort of "commented out" code left in a program is a
big mistake. If the code is soething that does need to stay
for optional use, then it needs to be properly integrated along
with logic to control when it's used.
 
M

Michael Rudolf

Am 25.02.2010 17:39, schrieb Grant Edwards:
IMO, any sort of "commented out" code left in a program is a
big mistake. If the code is soething that does need to stay
for optional use, then it needs to be properly integrated along
with logic to control when it's used.

OK, then we are perfectly fine and of course for personal use everyone
can "comment out" code as they wish.

I'd just hate to see something like "if False" in production level code.
 
M

Michael Rudolf

Am 26.02.2010 12:47, schrieb Michael Rudolf:
I'd just hate to see something like "if False" in production level code.

And yeah, I've seen it. And worse.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top