将变量与 None 进行比较的正确方法

The proper way to compare a variable with None

测试 NoneType 变量的常用方法是检查它是否引用 None 单调:

test_var is None

根据 PEP-8:

,建议将此方法作为检查 None 的唯一方法

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

尽管有时我会在不同的来源中找到以下测试:

isinstance(test, type(None))

这种方式看起来不错,但我不太明白为什么可以用 isinstance 代替简单易读的结构。它对某些情况更有用还是可以参考codestyle?

UPD: 唯一使用 isinstance 检查的 NoneType 我发现非常有用(到目前为止):

isinstance(None, (NoneType, str, float))

不,从来没有使用该结构的理由。

您不能继承 NoneType。唯一能通过该测试的对象是 None 单例。该行可能是由不知道这一点并认为他们也需要测试子类的人写的。