0

I am trying to follow PEP-8 in my project.
E.g. 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?

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
pospolitaki
  • 313
  • 2
  • 9
  • 6
    1) you can put it before the line as a [block comment](https://www.python.org/dev/peps/pep-0008/#block-comments), 2) PEP8 is not a holy scripture. – bereal Apr 11 '20 at 11:01

2 Answers2

2

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!")
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • docstrings aren't useful for comments about small number of lines of code. – martineau Apr 11 '20 at 12:09
  • @martineau Agreed. Please don't go on the example I pasted in my answer. This was just to let OP know that docstrings are also an option. And they can be rather useful in something like defining a unit test. – Mayank Porwal Apr 11 '20 at 12:44
1

I guess you could just try breaking the comments into multiple lines. This way you could work around the actual 79 chars rule.