Python “x 不在”与“x 不在”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3481554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"x not in" vs. "not x in"
提问by avacariu
I've noticed that both of these work the same:
我注意到这两者的工作方式相同:
if x not in listand if not x in list.
if x not in list和if not x in list。
Is there some sort of difference between the two in certain cases? Is there a reason for having both, or is it just because it's more natural for some people to write one or the other?
在某些情况下,两者之间是否存在某种差异?是否有理由同时拥有两者,或者仅仅是因为某些人写一个或另一个更自然?
Which one am I more likely to see in other people's code?
我更可能在其他人的代码中看到哪一个?
采纳答案by Alex Martelli
The two forms make identical bytecode, as you can clearly verify:
这两种形式生成相同的字节码,您可以清楚地验证:
>>> import dis
>>> dis.dis(compile('if x not in d: pass', '', 'exec'))
1 0 LOAD_NAME 0 (x)
3 LOAD_NAME 1 (d)
6 COMPARE_OP 7 (not in)
9 JUMP_IF_FALSE 4 (to 16)
12 POP_TOP
13 JUMP_FORWARD 1 (to 17)
>> 16 POP_TOP
>> 17 LOAD_CONST 0 (None)
20 RETURN_VALUE
>>> dis.dis(compile('if not x in d: pass', '', 'exec'))
1 0 LOAD_NAME 0 (x)
3 LOAD_NAME 1 (d)
6 COMPARE_OP 7 (not in)
9 JUMP_IF_FALSE 4 (to 16)
12 POP_TOP
13 JUMP_FORWARD 1 (to 17)
>> 16 POP_TOP
>> 17 LOAD_CONST 0 (None)
20 RETURN_VALUE
so obviously they're semantically identical.
所以很明显它们在语义上是相同的。
As a matter of style, PEP 8does not mention the issue.
就风格而言,PEP 8没有提到这个问题。
Personally, I strongly prefer the if x not in yform -- that makes it immediately clear that not inis a single operator, and"reads like English". if not x in ymay mislead some readers into thinking it means if (not x) in y, reads a bit less like English, and has absolutely no compensating advantages.
就我个人而言,我非常喜欢这种if x not in y形式——它立即表明它not in是一个单一的运算符,并且“读起来像英语”。 if not x in y可能会误导一些读者认为它的意思if (not x) in y,读起来不像英语,完全没有补偿优势。
回答by Ned Batchelder
It just personal preference. You could also compare if x != 3and if not x == 3. There's no difference between the two expressions you've shown.
这只是个人喜好。您还可以比较if x != 3和if not x == 3。您展示的两个表达式之间没有区别。
回答by habnabit
not x in Lisn't explicitly disallowed because that would be silly. x not in Lis explicitly allowed (though it compiles to the same bytecode) because it's more readable.
not x in L没有明确禁止,因为那很愚蠢。x not in L明确允许(尽管它编译为相同的字节码),因为它更具可读性。
x not in Lis what everyone uses, though.
x not in L但是,这是每个人都使用的。
回答by Mark Byers
When you write a not in bit is using the not inoperator, whereas not a in buses the inoperator and then negates the result. But the not inoperator is defined to return the same as not a in bso they do exactly the same thing. From the documentation:
当您编写a not in b它时使用not in运算符,而not a in b使用in运算符然后否定结果。但是not in运算符被定义为返回相同的内容,not a in b因此它们执行完全相同的操作。从文档:
The operators
inandnot intest for collection membership.x in sevaluates to true ifxis a member of the collections, and false otherwise.x not in sreturns the negation ofx in s.
运算符
in和not in集合成员资格的测试。x in s如果x是集合的成员,则评估为真,否则为s假。x not in s返回 的否定x in s。
Similarly there is a is not bversus not a is b.
同样有a is not bvs not a is b。
The extra syntax was added because it makes it easier for a human to read it naturally.
添加了额外的语法是因为它使人们更容易自然地阅读它。
回答by killown
>>> dis.dis(lambda: a not in b)
1 0 LOAD_GLOBAL 0 (a)
3 LOAD_GLOBAL 1 (b)
6 COMPARE_OP 7 (not in)
9 RETURN_VALUE
>>> dis.dis(lambda: not a in b)
1 0 LOAD_GLOBAL 0 (a)
3 LOAD_GLOBAL 1 (b)
6 COMPARE_OP 7 (not in)
9 RETURN_VALUE
when you do "not a in b" it will need be converted for (not in)
当你做“not a in b”时,它需要被转换为(not in)
so, the right way is "a not in b".
所以,正确的方法是“a not in b”。

