How to break a line in a function definition in Python according to pep8?

Question:

I have the following line of code that is just over the limit of 79 characters:

def ReportResults(self, intTestID, startTime, stopTime, version, serverType):

How do I break the line in the correct way according to pep8?

Asked By: Michael

||

Answers:

According to PEP8:

The Python standard library is conservative and requires limiting lines to 79 characters (and docstrings/comments to 72).

This is in my opinion the main rule to observe.

Besides this rule, PEP8 recommends aligning brackets, so I would do something like this:

def report_results(self,
                   intTestID,
                   startTime,
                   stopTime,
                   version,
                   serverType):
    pass

Note that I renamed your method report_results, following the recommended lower_case_with_underscores. Also, notice that the indentation should be aligned with the first letter of the first parameter and not the parenthesis.

Answered By: Right leg

79 characters is more of a guideline than a rule.

Some teams strongly prefer a longer line length. For code maintained exclusively or primarily by a team that can reach agreement on this issue, it is okay to increase the nominal line length from 80 to 100 characters (effectively increasing the maximum length to 99 characters), provided that comments and docstrings are still wrapped at 72 characters.

Additionally, that line is only 77 characters long, so you should be fine anyway. However, if you’d like to break it up, you can use implicit continuation:

def ReportResults(self,
                  intTestID,
                  startTime,
                  stopTime,
                  version,
                  serverType):

If you have a function signature that goes far past the character limit you’re using, that is an indication that the function has too many parameters.

Answered By: TigerhawkT3
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.