3

I have already read this: Why doesn't Python have multiline comments?

So in my IDLE , I wrote a comment:

Hello#World

Anything after the d of world is also a part of the comment.In c++ , I am aware of a way to close the comment like:

/*Mycomment*/

Is there a way to end a comment in Python?

NOTE: I would not prefer not to use the triple quotes.

1
  • 4
    No, there isn't. Why would you want to? Commented Feb 28, 2015 at 17:32

4 Answers 4

3

You've already read there are no multiline comments, only single line. Comments cause Python to ignore everything until the end of the line. You "close" them with a newline!

I don't particularly like it, but some people use multiline strings as comments. Since you're just throwing away the value, you can approximate a comment this way. The only time it's really doing anything is when it's the first line in a function or class block, in which case it is treated as a docstring.

Also, this may be more of a shell scripting convention, but what's so bad about using multiple single line comments?

#####################################################################
# It is perfectly fine and natural to write "multi-line" comments   #
# using multiple single line comments. Some people even draw boxes  #
# with them!                                                        #
#####################################################################
Sign up to request clarification or add additional context in comments.

4 Comments

"Since you're just producing a value and then throwing it away": FWIW, you're not producing a value in CPython -- non-docstring string literals generate no bytecode.
Point taken. Poor word choice, perhaps? To me, it feels unusual to have a literal in a program that is not named or used. Maybe that's just a style thing. That's all I meant, in any case. @DSM Updated wording.
@DSM: No byte-code is produced, but the byte code compiler "wastes" time and resources processing the multi-line string literal before throwing it away (assuming it wasn't a docstring). Personally that doesn't bother me much especially since Python uses multi-line string literals for precisely this purpose (commenting code) itself and that most modules are only compiled the first time they're encountered.
@martineau: given that Guido himself has endorsed it, I think we're on safe ground in doing so. :-)
1

You can't close a comment in python other than by ending the line.

There are number of things you can do to provide a comment in the middle of an expression or statement, if that's really what you want to do.

First, with functions you annotate arguments -- an annotation can be anything:

def func(arg0: "arg0 should be a str or int", arg1: (tuple, list)):
    ...

If you start an expression with ( the expression continues beyond newlines until a matching ) is encountered. Thus

assert (
    str
    # some comment
    .
    # another comment
    join
) == str.join

Comments

0

You can emulate comments by using strings. They are not exactly comments, since they execute, but they don't return anything.

print("Hello", end = " ");"Comment";print("World!")

Comments

-1

if you start with triple quotes, end with triple quotes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.