用行延续进行注释

2024-10-01 07:23:49 发布

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

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

Tags: andthe代码lenif指南建议next
2条回答

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

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.

在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.

下面是一个演示:

^{pr2}$

relevant PEP 8 guideline是:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

相关问题 更多 >