跳到内容

if-with-same-arms (SIM114)

源自 flake8-simplify 代码检查器。

有时提供修复。

作用

检查具有相同分支体的 if 分支。

为什么这不好?

如果 if 语句的多个分支具有相同的主体,使用 or 更好地表达了语句的意图。

示例

if x == 1:
    print("Hello")
elif x == 2:
    print("Hello")

建议改为

if x == 1 or x == 2:
    print("Hello")