为什么python使用非常规的三引号进行注释?
发布于 2021-01-29 17:55:33
为什么python不只使用C / C ++ / Java使用的传统注释风格:
/**
* Comment lines
* More comment lines
*/
// line comments
// line comments
//
是有特定原因吗?还是仅仅是武断?
关注者
0
被浏览
50
1 个回答
-
Python不会在注释中使用三引号。注释使用井号(又称井号):
# this is a comment
三引号是一个文档字符串,与注释不同,它实际上可以作为程序的真实字符串使用:
>>> 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
只要是字符串,就不严格要求使用三引号。使用
"""
只是一个惯例(并且具有多行的优势)。