[Python-ideas] Comment syntax similar to /* */ in C?

Brett Cannon brett at python.org
Sun Oct 18 18:45:58 CEST 2009


On Sun, Oct 18, 2009 at 07:53, Steven D'Aprano <steve at pearwood.info> wrote:

> On Mon, 19 Oct 2009 12:40:01 am Sturla Molden wrote:
>
> > In Python a comment start from # and spans the rest of the line,
> > similar to // in C++.
> >
> > In C/C++, we can also make comments that don't span the rest of the
> > line, but has two delimiters:
> >
> >
> > /* comment */
> >
> >
> > This allows us to write:
> >
> >
> > foobar(a, b /* comment on b */, c, d, e, f);
>
> Ewwww. That's horrible. If you ask me, in-expression comments are a
> misfeature the world would be better off without.
>
> > I have sometimes missed this possibility when writing Python code. To
> > my knowledge there is no equivalent to /* */ in Python.
> >
> > I don't care much about choice of delimiter. Perhaps ### comment ###
> > is the most intuitive, as it has resemblance to triple-quotes for
> > text strings? It also stands out clearly in black/white text.
> >
> >
> > foobar(a, b ### comment on b ###, c, d, e, f)
> >
> > foobar(a, b ### comment on b
> >   that spans two lines ###, c, d, e, f)
>
> Take advantage of Python's rules for handling open parentheses:
>
> foobar(a, b, # comment on b
>  c, d, e, f)
>
> foobar(a, b, # comment on b
>  # more and longer comment on b
>  c, d, e, f)
>
>
> And don't forget that there's no need for comments to be on the same
> physical line they are referring to. You are allowed to include
> comments surrounding the line.
>
>
> > ###
> > comment that spans multiple lines
> > ###
>
> Use a modern editor that lets you easily comment and uncomment multiple
> lines at once.
>
>
>
> > P.S. In Python we can emulate multiline comments using triple-quoted
> > strings, but conceptually strings and comments are very different.
> > I.e. strings are objects, comments are auxillary text discarded at
> > compile time. Strings are objects created at runtime, comments are
> > not.
>
> Guido's time-machine strikes again.
>
> >>> import dis
> >>> def test():
> ...     x = 1
> ...     """
> ...     This is a triple-quote comment.
> ...     """
> ...     return x
> ...
> >>> dis.dis(test)
>  2           0 LOAD_CONST               1 (1)
>              3 STORE_FAST               0 (x)
>
>  6           6 LOAD_FAST                0 (x)
>              9 RETURN_VALUE
>
>
> String literals -- not just triple-quoted strings, but any string
> literal -- that don't go anywhere are discarded by the Python compiler,
> precisely so they can be used as comments.
>

And because of this new syntax will never be added for multi-line comments.
You guys can keep talking, but this is one of those "over Guido's
resignation" type of proposals.

-Brett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091018/63ddc9cf/attachment.html>


More information about the Python-ideas mailing list