[Python-Dev] Can we triple quoted string as a comment?

Raymond Hettinger raymond.hettinger at gmail.com
Tue Mar 26 02:16:47 CET 2013

On Mar 25, 2013, at 2:16 PM, Victor Stinner <victor.stinner at gmail.com> wrote:

> Hi,
> I just realized that the Python peephole optimizer removes useless
> instructions like numbers and strings between other instructions,
> without raising an error nor emiting an error. Example:
> $ python -Wd -c 'print "Hello"; "World"'
> Hello

IIRC, this happens upstream from the peephole optimizer
and has been a part of Python for a long time.

You can also "comment-out" code with "if 0:"

>>> def f(x):
	if 0:
		print x
	return x+1

>>> from dis import dis
>>> dis(f)
  4           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (1)
              6 BINARY_ADD          
              7 RETURN_VALUE 

> As part of my astoptimizer project, I wrote a function to detect such
> useless instructions which emit a warning. I opened the following
> issue to report what I found:
> http://bugs.python.org/issue17516

Make sure it is a warning you can turn-off.

I've seen code in many organizations that use multi-line
strings to "turn-off" a section of code but not actually remove the code
from the source.

> Different modules use long strings as comments. What is the "official"
> policy about such strings? Should we use strings or comments?
> (IMO a comment should be used instead.)

The module authors typically make their own decisions
with respect to readability and ease of commenting.

If you're editing with Emacs, it is really easy to reflow paragraphs
and to insert or remove multiline comments each prefixed with #.
But with other editors, it can be a PITA and a multiline string is
the easiest to maintain and works well when cutting-and-pasting
the comments from somewhere else.

I worry that because you just discovered this feature, the initial
reaction is that is a horribly wrong thing to do and should be
"fixed" everywhere.

Instead, it would be better to live-and-let live.
No need for wholesale code changes or imposition
"you must do it the way I do it" policies.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130325/7baca9e5/attachment-0001.html>

More information about the Python-Dev mailing list