跳到内容

if-stmt-min-max (PLR1730)

源自 Pylint 代码检查工具。

有时提供修复。

作用

检查可以被 min()max() 调用替换的 if 语句。

为什么这不好?

选择两个子表达式中较小或较大值的 if 语句可以分别用 min()max() 调用替换。如果可能,请优先使用 min()max(),因为它们比等效的 if 语句更简洁易读。

示例

if score > highest_score:
    highest_score = score

建议改为

highest_score = max(highest_score, score)

修复安全性

如果此修复会删除替换范围内的任何注释,则会被标记为不安全。

一个例子来说明注释在哪些情况下会被保留,哪些情况下不会

a, b = 0, 10

if a >= b: # deleted comment
    # deleted comment
    a = b # preserved comment

参考