简体   繁体   中英

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

I am trying to follow PEP-8 in my project.
Eg there is some statement which should have a long comment (so line length more than 79 characters now):

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

In PEP-8 we read:

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?

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.

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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