评论续行

时间:2014-11-18 02:54:13

标签: python python-2.7 comments pep8 continuation

我有这个代码块我想发表评论,但内联评论不起作用。我不确定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.

2 个答案:

答案 0 :(得分: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
>>>

relevant PEP 8 guideline是:

  

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

答案 1 :(得分: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.