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

2024-09-28 01:32:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我的以下代码行超出了79个字符的限制:

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

根据pep8,我如何以正确的方式突破底线?在


Tags: 代码selfversiondef方式starttimepep8个字符
2条回答

根据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):

注意,我重命名了您的方法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):

如果函数签名远远超过了所使用的字符限制,则表示函数的参数太多。在

相关问题 更多 >

    热门问题