python使用三引号来注释的具体原因? [英] Specific reason why python uses triple-quotation marks for comments?

查看:1181
本文介绍了python使用三引号来注释的具体原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有python只是使用传统风格的评论,如C / C ++ / Java使用:

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

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

// line comments
// line comments
//

这是有具体原因还是只是任意?

Is there a specific reason for this or is it just arbitrary?

推荐答案

Python不会对注释使用三重引号。注释使用哈希(aka pound)字符:

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

# this is a comment

三重引号是一个 doc字符串,并且与注释不同,实际上作为程序的真实字符串可用:

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).

这篇关于python使用三引号来注释的具体原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆