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

Laurent Pointal laurent.pointal at laposte.net
Wed Mar 18 14:04:28 EDT 2015


Aditya Raj Bhatt wrote:

> I always do single line comments with # but just for the sake of it I
> tried it with ''' ''' and it gives me a syntax error.
> 
> In both the interpreter, and the source code text file, doing -
> 
> a = 5 '''a comment'''
> 
> results in a syntax error, with the very last quote at the end of the
> line highlighted in red. Of course, if I do -
> 
> a = 5 #'''a comment'''
<zip>
> 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.

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

You can have an expression alone, with no stored result (ie. no assignment 
to a name), and this is how are used triple quoted strings as comments.

a = 5
"""a comment"""

Take care of indent:

def f(x):
    a = 5
    """an correctly indented expression to be
    inside the function"""
    return a * x

A+
Laurent.




More information about the Python-list mailing list