如何在 Python 中正确地向语句添加长注释?

How to add a long comment to a statement properly in Python?

我正在尝试在我的项目中遵循 PEP-8
例如。有一些语句应该有一个长注释(所以行长度现在超过 79 个字符):

    fields_to_display = ['id', 'order_item']  # set here a list of fields to display when something happens somewhere

PEP-8中我们读到:

An inline comment is a comment on the same line as a statement.

所以我想我应该使用内嵌注释。 如果是这样,我应该如何使其适合 79 characters restriction

是的,你应该尽量让所有的行都在 79 chars 之内,只需小心地放置特定的关键字来解释它,而不是使用正确的英语句子。

但是,如果您必须有一个很长的描述,您可以将注释分成多行。

""" Multi-line comment used 
here for my code. """

PEP-8也说,

Inline comments are unnecessary and in fact distracting if they state the obvious.

所以,最好在你的方法定义下面有 docstrings 来解释方法,而不是把它放在每一行上。像这样:

def greet(name):
    """
    This function greets to
    the person passed in as
    a parameter
    """
    print("Hello, " + name + ". Good morning!")

我想您可以尝试将评论分成多行。这样您就可以解决实际的 79 chars 规则。