以前的开发已经离开代码中的一个很奇怪的not x is None
尤达条件:与尤达条件“没x是无”
>>> x = None
>>> not x is None
False
>>> x = 1
>>> not x is None
True
一些测试后,我似乎相同的输出x is not None
。
>>> x = None
>>> not x is None
False
>>> x is not None
False
>>> x = 1
>>> not x is None
True
>>> x is not None
True
是not x is None
总是等同于x is not None
?
打破了这种状况,是not (x is None)
或(not x) is None
?或者前者总是等于后者?
这都是关于运算符的优先级。阅读这个答案http:// stackoverflow。com/questions/31421379/why-does-not-true-false-false-true-return-false/31458009#31458009 – Kasramvd
是按照[文档](https://docs.python.org/3/reference/expressions。 html#operator-precedence),** not **的优先级低于compare **的是**。所以你以前的假设应该是正确的。 – umutto
“Yoda条件”将是'None is x'。 – Ryan