“x不在y中”或“y中没有x”
在测试成员资格时,我们可以使用:
x not in y
或者:
not x in y
该表达式可以有许多可能的上下文,具体取决于 x
和 y
。例如,它可以用于子字符串检查、列表成员身份、字典键是否存在。
- 这两种形式总是等价的吗?
- 有首选语法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
他们总是给出相同的结果。
事实上,
not 'ham' in 'spam and Eggs'
似乎是执行单个“not in”操作的特殊情况,而不是执行“in”操作然后否定结果:我有起初认为它们总是给出相同的结果,但
not
本身只是一个低优先级逻辑否定运算符,它可以应用于a in b
就像与任何其他布尔表达式一样容易,而为了方便和清晰起见,not in
是一个单独的运算符。上面的拆解已经暴露了!看起来,虽然
not
显然是一个逻辑否定运算符,但not a in b
的形式是特殊情况,因此它实际上并未使用通用运算符。这使得not a in b
在字面上与a not in b
表达式相同,而不仅仅是产生相同值的表达式。They always give the same result.
In fact,
not 'ham' in 'spam and eggs'
appears to be special cased to perform a single "not in" operation, rather than an "in" operation and then negating the result:I had thought at first that they always gave the same result, but that
not
on its own was simply a low precedence logical negation operator, which could be applied toa in b
just as easily as any other boolean expression, whereasnot in
was a separate operator for convenience and clarity.The disassembly above was revealing! It seems that while
not
obviously is a logical negation operator, the formnot a in b
is special cased so that it's not actually using the general operator. This makesnot a in b
literally the same expression asa not in b
, rather than merely an expression that results in the same value.<块引用>
运算符
not in
被定义为具有in
的逆真值。—Python 文档
not in
是首选,因为它更明显他们还为其添加了一个特殊情况。not in
is preferred because it is more obvious and they added a special case for it.它们的含义相同,但 pycodestyle Python 样式指南检查器(以前称为 pep8) 更喜欢
规则 E713 中的 >not in
运算符:另请参阅“Python
if x is not None
或if not x is None
?” 用于非常相似的样式选择。They are identical in meaning, but the pycodestyle Python style guide checker (formerly called pep8) prefers the
not in
operator in rule E713:See also "Python
if x is not None
orif not x is None
?" for a very similar choice of style.其他人已经非常清楚地表明,这两种说法在相当低的水平上是等效的。
但是,我认为还没有人充分强调,由于这将选择权留给了您,因此您应该
选择使代码尽可能具有可读性的形式。
并且不一定像那样具有可读性对任何人来说都是可能的,即使这当然是一件好事。不,请确保代码对您来说尽可能可读,因为您是最有可能稍后再回到此代码并尝试阅读它的人。
Others have already made it very clear that the two statements are, down to a quite low level, equivalent.
However, I don't think that anyone yet has stressed enough that since this leaves the choice up to you, you should
choose the form that makes your code as readable as possible.
And not necessarily as readable as possible to anyone, even if that's of course a nice thing to aim for. No, make sure the code is as readable as possible to you, since you are the one who is the most likely to come back to this code later and try to read it.
在Python中,没有区别。并且没有任何偏好。
In Python, there is no difference. And there is no preference.
从语法上讲,它们是相同的语句。我会很快指出,“火腿”不在“垃圾邮件和鸡蛋”中传达了更清晰的意图,但我已经看到了代码和场景,其中“火腿”不在“垃圾邮件和鸡蛋”中' 传达的含义比另一个更清晰。
Syntactically they're the same statement. I would be quick to state that
'ham' not in 'spam and eggs'
conveys clearer intent, but I've seen code and scenarios in whichnot 'ham' in 'spam and eggs'
conveys a clearer meaning than the other.