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?

jwalton
  • 5,286
  • 1
  • 18
  • 36
  • Kinda begs the question, why are you writing multi-line comments in the terminal? – doctorlove Jun 12 '19 at 09: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. – jwalton Jun 12 '19 at 09:41

3 Answers3

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.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 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 ```str```ing literal evaulation doesn't "do anything"? This is in contrast to actual comments, which are completely ignored and never evaluated. – jwalton Jun 14 '19 at 09:48
  • 1
    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. – ForceBru Jun 14 '19 at 09:53
  • Thanks for your insight, this clears up my confusion and answers my question. – jwalton Jun 14 '19 at 09:55
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

Ardiya
  • 677
  • 6
  • 19
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.

alec_djinn
  • 10,104
  • 8
  • 46
  • 71