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

后端 未结 5 767
我在风中等你
我在风中等你 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:29

    Triple-quotes aren't comments. They're string literals that span multiple lines and include those line breaks in the resulting string. This allows you to use

    somestr = """This is a rather long string containing
    several lines of text just as you would do in C.
        Note that whitespace at the beginning of the line is\
     significant."""
    

    instead of

    somestr = "This is a rather long string containing\n\
    several lines of text just as you would do in C.\n\
        Note that whitespace at the beginning of the line is\
     significant."
    

提交回复
热议问题