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

Tim Roberts timr at probo.com
Sat Mar 28 19:34:51 EDT 2015


Aditya Raj Bhatt <adityarajbhatt at gmail.com> 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'''
>
>?
> 
>> 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?

Were you ever able to extract the Real Answer to your original question
from the froth that resulted?  Your basic misunderstanding is that the
triple-quote thing is NOT a comment marker.  It is a string literal,
exactly like a single-quoted string, except that it allows embedded
newlines.  So, your statement
    a = 5 '''a comment'''
is invalid for exactly the same reason that the statement
    a = 5 "a comment"
is invalid.  Neither one of those statement have any comments.

There is a CONVENTION to embed a literal string as the first line in a
function, to allow for automatic documentation.  Whether the literal string
is single-quoted or triple-quoted is irrelevant.  That is, these two things
are equivalent:

    def func(a):
        "This is a function"
        return a*2

    def func(a):
        """This is a function"""
        return a*2
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list