问题
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