A simple single line, triple-quoted comment is giving syntax error. Why?

Chris Angelico rosuav at gmail.com
Wed Mar 18 19:23:00 EDT 2015


On Thu, Mar 19, 2015 at 9:58 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> But let's also remember that sometimes you cannot be bothered with *best*
> practice, and "good enough for now" practice is fine.
>
> Need to comment out a large block of code in a hurry while using a basic
> editor that doesn't offer a "Comment" and "Uncomment" command? Best
> practice says I should use a better editor, and that's fine, but right this
> instant I'm using the tools I have, not the tools I want, and the easiest
> way to fix this is to surround the block of code with triple-quotes. I can
> always come back and fix it later. Famous last words, I know, but who among
> us can truthfully say they have *never* taken a quick and dirty short-cut
> while programming?

There are three broad types of comments:

1) Single-line or to-end-of-line comments, started with a # or a // or
something. Python supports these, they're easy to work with and
recognize. No problem.
2) Block comments, consisting of a number of entire lines of either
text or commented-out code.
3) Partial-line comments - /* comment */ in C-like languages.

You can abuse triple-quoted strings to do #2, but not #3. For
instance, in C, you can legally do something like this:

if (condition1 /* && condition2 */) do_stuff();

In terms of "quick and dirty short-cuts", that's one that I miss
having a mechanic for in Python. And yes, you can decry it as a bad
practice, but just as with Steven's example, it's always with the plan
to fix it later (usually by making the code more flexible in some
way), and yet does sometimes stay in the code for a rather long time,
for the same reason that several of my Alice posters are currently
lying face down on the Alice Shelf instead of being up on the wall -
there's nothing more permanent than a temporary solution.

(There's also technically a fourth use of slash-star comments -
beginning part way into one line and ending part way into another
line:

code code /* blah blah
blah blah
blah */ more code

But if you do this on a regular basis, I think we are justified in
throwing rotten fruit at you.)

ChrisA



More information about the Python-list mailing list