Why does python use unconventional triple-quotation marks for comments?

后端 未结 5 774
我在风中等你
我在风中等你 2020-12-07 17:54

Why didn\'t python just use the traditional style of comments like C/C++/Java uses:

/**
 * Comment lines 
 * More comment lines
 */

// line comments
// line         


        
5条回答
  •  误落风尘
    2020-12-07 18:21

    Python doesn't use triple quotation marks for comments. Comments use the hash (a.k.a. pound) character:

    # this is a comment
    

    The triple quote thing is a doc string, and, unlike a comment, is actually available as a real string to the program:

    >>> def bla():
    ...     """Print the answer"""
    ...     print 42
    ...
    >>> bla.__doc__
    'Print the answer'
    >>> help(bla)
    Help on function bla in module __main__:
    
    bla()
        Print the answer
    

    It's not strictly required to use triple quotes, as long as it's a string. Using """ is just a convention (and has the advantage of being multiline).

提交回复
热议问题