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

限于喜欢 提交于 2019-12-10 19:55:13

问题


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?


回答1:


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:

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

Note that I renamed your method report_results, following the recommended lower_case_with_underscores.




回答2:


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.



来源:https://stackoverflow.com/questions/40542837/how-to-break-a-line-in-a-function-definition-in-python-according-to-pep8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!