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.

Community
  • 1
  • 1
m0bi5
  • 8,900
  • 7
  • 33
  • 44

4 Answers4

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!                                                        #
#####################################################################
Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
  • 2
    "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. – DSM Feb 28 '15 at 17:43
  • 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. – Two-Bit Alchemist Feb 28 '15 at 17:57
  • 1
    @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 Feb 28 '15 at 18:31
  • @martineau: given that Guido himself has endorsed it, I think we're on safe ground in doing so. :-) – DSM Feb 28 '15 at 19:30
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
Dunes
  • 37,291
  • 7
  • 81
  • 97
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!")
-2

if you start with triple quotes, end with triple quotes