0

我有这段代码要评论,但内联评论不起作用。我不确定这里适用什么 PEP8 指南。建议?

        if next_qi < qi + lcs_len \ # If the next qLCS overlaps 
        and next_ri < ri + lcs_len \ # If the next rLCS start overlaps 
        and next_ri + lcs_len > ri: # If the next rLCS end overlaps
            del candidate_lcs[qi] # Delete dupilicate LCS.
4

2 回答 2

6

在 Python 中,在\续行符之后没有任何内容。

但是,如果您将条件放在括号中,您可以做您想做的事情:

if (next_qi < qi + lcs_len   # If the next qLCS overlaps 
and next_ri < ri + lcs_len   # If the next rLCS start overlaps 
and next_ri + lcs_len > ri): # If the next rLCS end overlaps
    del candidate_lcs[qi] # Delete dupilicate LCS.

下面是一个演示:

>>> if (1 == 1   # cond 1
... and 2 == 2   # cond 2
... and 3 == 3): # cond 3
...     print True
...
True
>>>

相关的PEP 8 指南是:

包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐含行继续。通过将表达式括在括号中,可以将长行分成多行。这些应该优先使用反斜杠来继续行。

于 2014-11-18T02:55:47.067 回答
1

处理非常长的行的一个经常被忽视的方法是将它们分成更多、更短的行:

q_overlaps = next_qi < qi + lcs_len          # If the next qLCS overlaps 
r_start_overlaps = next_ri < ri + lcs_len    # If the next rLCS start overlaps 
r_end_overlaps = next_ri + lcs_len > ri      # If the next rLCS end overlaps
if q_overlaps and r_start_overlaps and r_end_overlaps:
    del candidate_lcs[qi] # Delete dupilicate LCS.
于 2014-11-18T03:26:27.383 回答