2

Everywhere I look tells me that a multiline comment can be created as:

'''
This is a multiline
comment.
'''

(see eg this answer, and many more).

However, when I execute this in a python or ipython terminal I see my multiline 'comment' printed.

>>> '''
... This is a multiline
... comment.
... '''
'\nThis is a multiline\ncomment.\n'
>>> 

This was not the behaviour I expected. I was led to believe the above code was equivalent to using hashes to denote comments:

>>> # This is also a multiline
... # comment.
... 
>>> 

which, as I expected, doesn't print anything at all.

So what gives here? Everywhere is telling me I can create multiline comments with the ''' or """ syntax. But, when I'm working directly in a terminal, I don't observe this supposed behaviour.

Is the behaviour in my first example because my comment was interpreted to be a docstring and was therefore printed?

2
  • Kinda begs the question, why are you writing multi-line comments in the terminal? Commented Jun 12, 2019 at 9:40
  • @doctorlove This is besides the point. Of course I'm not really using multi-line comments in the terminal. I just expected the above to work. Commented Jun 12, 2019 at 9:41

3 Answers 3

5

That's because it's a multi line string literal, not a multi line comment. It can be used as a multi line comment though, because, just as a comment, it doesn't "do anything", and it seems that it's ignored, just like a comment.

However, as you observed, the string literal actually evaluates to a string object with all the newline characters and stuff. Comments, on the other hand, are ignored completely and aren't evaluated to anything.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I'm just trying to verify I understand this answer. So, the multi line comment isn't actually ignored and is actually evaluated? It only seems to be ignored, because the string literal evaulation doesn't "do anything"? This is in contrast to actual comments, which are completely ignored and never evaluated.
The "multiline comment" is actually an ordinary string, and it's evaluated just like any other string. However, if you put, say, "hello, world" in the middle of your script (but properly indented), it won't alter the environment. But this kind of string literal can only be one line, while multiline string literals can span multiple lines. Actual comments are removed by the parser, so the language runtime doesn't see them at all.
Thanks for your insight, this clears up my confusion and answers my question.
1

''' and """ are actually for multiline string. # is telling the interpreter to skip the rest of the line.

You are currently running it in REPL, so ''' and """ would return a string, and it will be shown in your REPL.

If you are running it in python program, such as python [filename.py] it will not be shown, unless you use print "Hello world"

Also, the ''' and """ commonly use as multiline docstring in PEP guideline, see https://www.python.org/dev/peps/pep-0257/#id17

Comments

0

Triple-quoted text is considered a string in Python, not a comment. Try '''I am a triple-quoted string'''.split(), split() will work just fine since the object is a string.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.