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

Laurent Pointal laurent.pointal at laposte.net
Wed Mar 18 15:08:13 EDT 2015


Aditya Raj Bhatt wrote:

> On Wednesday, March 18, 2015 at 1:04:39 PM UTC-5, Laurent Pointal wrote:
>> > Can someone also provide a sort of a 'guide' to triple-quoted
> comments
>> > in general?
>> 
>> A triple ' or " string is a Python string, allowing line-return in
> string.
> 
> What do you mean by line-return in string? Is it newline? Does it mean I
> can write -
> 
> '''first part
> second part'''
> 
> ?

Yes.

>> If it is in an expression (like a = 5 '''a comment'''), then it must
> be a
>> valid expression (and here it is not).
> 
> What is not a valid expression here?

What do you expect as result of this combination of an integer with a 
string?

>>> a = 5 '''a comment'''
  File "<stdin>", line 1
    a = 5 '''a comment'''
                        ^
SyntaxError: invalid syntax

You may use an *, and you buid a valid expression with a result

>>> a = 5 * '''a comment'''

But… this may not be the value you wanted for a variable.

>>> a
'a commenta commenta commenta commenta comment'

So, a string is really not a comment, even if triple quote strings can be 
used alone to store multi-line "comments".

A+
Laurent.




More information about the Python-list mailing list