# Python 并不存在多行注释

2018/12/03 18:43

### 前言

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

Paragraphs inside a block comment are separated by a line containing a single #.

An inline comment is a comment on the same line as a statement. Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.

Inline comments are unnecessary and in fact distracting if they state the obvious. Don't do this:

x = x + 1                 # Increment x

But sometimes, this is useful:

x = x + 1                 # Compensate for border

x = x + 1    # Increment x

x = x + 1   # Compensate for border

### Documentation Strings

Conventions for writing good documentation strings (a.k.a. "docstrings") are immortalized in PEP 257.

Write docstrings for all public modules, functions, classes, and methods. Docstrings are not necessary for non-public methods, but you should have a comment that describes what the method does. This comment should appear after the def line.

PEP 257 describes good docstring conventions. Note that most importantly, the """ that ends a multiline docstring should be on a line by itself:

"""Return a foobang

Optional plotz says to frobnicate the bizbaz first.

"""

For one liner docstrings, please keep the closing """ on the same line.

1. 为所有公共模块、函数、类和方法编写文档字符串。对于非公共方法，docstrings 不是必需的，但是应该有一个注释来描述该方法的功能。这个注释应该出现在 def 行之后。

2. PEP 257描述了良好的文档字符串约定。注意，最重要的是，结束多行文档字符串的 """ 应该单独在一行上:

"""Return a foobangOptional plotz says to frobnicate the bizbaz first."""
1. 对于一行 docstrings，请保持关闭 """ 在同一行。

### 结论

pep8 中关于注释提提及的唯一注释方法就是使用 # 字符串去注释, 并没有提及用 """ 或者 ''' 作为多行注释使用,不过 python 的创造者对此也说了一句话:

Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! :-)

0
0 收藏

0 评论
0 收藏
0