如何根据 pep8 在 Python 中的函数定义中换行?

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

我有以下代码行刚好超过 79 个字符的限制:

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

如何根据pep8以正确的方式断行?

根据PEP8

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

我认为这是要遵守的主要规则。

除此规则外,PEP8 建议对齐括号,所以我会这样做:

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

请注意,我按照建议 lower_case_with_underscores 重命名了您的方法 report_results

79个字符是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.

此外,该行只有 77 个字符长,因此您应该没问题。但是,如果您想将其分解,可以使用隐式延续:

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

如果您的函数签名远远超出了您使用的字符限制,则表明该函数的参数过多。