简体   繁体   English

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

[英]How to add a long comment to a statement properly in Python?

I am trying to follow PEP-8 in my project.我正在尝试在我的项目中遵循PEP-8
Eg there is some statement which should have a long comment (so line length more than 79 characters now):例如,有一些语句应该有很长的注释(所以现在行长度超过 79 个字符):

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

In PEP-8 we read:PEP-8中,我们读到:

An inline comment is a comment on the same line as a statement.内联注释是与语句在同一行上的注释。

So i guess i should use an inline comment.所以我想我应该使用内联注释。 If so how should i make it fit the 79 characters restriction properly?如果是这样,我应该如何使它适合79 characters restriction

Yes, you should try to make all lines fit within 79 chars by just carefully putting specific keywords to explain it, rather than using a proper English sentence.是的,您应该尝试通过仔细放置特定关键字来解释它,而不是使用正确的英文句子,从而使所有行都适合79 chars

But, if you have to have a long description, you can break comments in multiple lines.但是,如果您必须有很长的描述,您可以将注释分成多行。

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

Well PEP-8 also says,好吧, PEP-8还说,

Inline comments are unnecessary and in fact distracting if they state the obvious.内联注释是不必要的,实际上如果它们明显地分散注意力的话。

So, it's better to have docstrings under your method definition explaining the method, rather than putting it on each line.因此,最好在方法定义下使用docstrings来解释方法,而不是将其放在每一行上。 Something like this:像这样的东西:

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

I guess you could just try breaking the comments into multiple lines.我想您可以尝试将评论分成多行。 This way you could work around the actual 79 chars rule.这样您就可以解决实际的79 chars规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM