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

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Feb 24 19:51:20 EST 2010


On Wed, 24 Feb 2010 18:18:27 +0000, kj wrote:

> 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.



-- 
Steven



More information about the Python-list mailing list