python - 在python中评论命令的正确方法

标签 python comments

<分区>

我知道在 python 中留下评论的两种方法。一个是使用 """,另一个是使用 #。我知道第一个可以用来返回一个函数帮助作为一个好处。但是我什么时候应该使用一个什么时候?还有我必须如何发表评论?我是否必须按 Tab 键并在第一行评论的下方加上命令?还是必须从该行的开头开始?

最佳答案

没有,只有one way of commenting, using # :

A comment starts with a hash character (#) that is not part of a string literal, and ends at the end of the physical line.

三重引号,""",创建一个字符串对象,当它是函数、模块或类的第一行时,它恰好被用作文档字符串。三重引号在许多其他地方也是如此,但不应与注释混淆。您可以像使用任何其他字符串文字一样使用三引号字符串,其具体好处是您可以在源代码中使用实际的换行符,而不必使用 \n 转义字符。

虽然它可以用于通过将代码块转换为多行字符串来禁用它,但您确实不应该这样做。使用适当的源代码控制并简单地删除 block ,或者使用允许您通过插入 # 为您注释掉整个 block 的编辑器。

对于实际注释,使用#Python style guide (PEP 8)关于何时以及如何使用评论有一些话要说;关于缩进有这样的说法:

Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).

关于python - 在python中评论命令的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17262319/

相关文章:

emacs - 如何在 Org 模式下进行内联注释?

css - 需要 FB 评论框滚动*在*我的位置 :fixed global navigation bar

mysql - 在 MySQL 语句中嵌入注释

ruby - 在好的 Ruby 代码中没有注释是否被认为是可以接受的?

python - 如何在单个 jupyter 单元格中显示多个 pandas describe() 输出?

python - gem 迷阵风格的游戏比赛

Python rpy2 - nls 回归 RRuntimeError

python - 使用 Scikit-learn 进行 RBF 拟合时出现 MemoryError

python - pandas shift 将我的列从整数转换为 float 。

vim - 当有注释掉的大括号时,如何使用 vim 缩进行?