Python 多行注释的 PEP8/最佳实践是什么?

What's the PEP8 / best practice for Python multiline comments?

我经常使用代码模式:

1    class myTest():
2        counter = 0      # use this as general counter
3        name_code = ''   # this is the name encoded using the
4                         # standard as defined in ...
5        category = ''    # category of test

第 4 行不是 PEP8 标准。 # 应该在位置 5,恕我直言,它看起来很丑:

3        name_code = ''   # this is the name encoded using the
4        #                  standard as defined in ...

你怎么评价这样的模式? 最佳做法是什么?

我会像 docstrings 那样做它们,因此它们实际上可用于 Sphinx 等其他工具,并且可用于例如生成文档:

class myTest():
    counter = 0
    """use this as general counter"""

    name_code = ''
    """this is the name encoded using the standard as defined in ..."""

    category = ''
    """category of test"""

文档字符串通常是多行字符串,因此可以根据需要扩展到多行。

例如,here's a class whose attributes I've documented with docstrings, allowing this 从中自动生成文档。

我会记录 class docstring 中的变量,因为它们是 class 的静态成员。这样您的实际代码会保持更紧凑,更易于阅读和理解:

class myTest():
    """Test class which does something.

    Attributes:
        counter (int): Use this as general counter.
        name_code (str): This is the name encoded using the
            standard as defined in this long piece of text.
            Just continue the lines with one level of indentation
            for as long as you want.
        category (str): Category of the test.

    """

    counter = 0
    name_code = ''
    category = ''

我在这里使用了 Google style Python docstring,因为这是我喜欢的风格。