“ x不在y”或“ x不在y”

“ x不在y”或“ x不在y”,第1张

“ x不在y”或“ x不在y”

他们总是给出相同的结果。

实际上,

not 'ham' in 'spam and eggs'
在特殊情况下,执行单个“非输入” *** 作而不是“输入” *** 作,然后取反结果:

>>> import dis>>> def notin():    'ham' not in 'spam and eggs'>>> dis.dis(notin)  20 LOAD_ConST    1 ('ham')   3 LOAD_ConST    2 ('spam and eggs')   6 COMPARE_OP    7 (not in)   9 POP_TOP    10 LOAD_ConST    0 (None)  13 RETURN_VALUE>>> def not_in():    not 'ham' in 'spam and eggs'>>> dis.dis(not_in)  20 LOAD_ConST    1 ('ham')   3 LOAD_ConST    2 ('spam and eggs')   6 COMPARE_OP    7 (not in)   9 POP_TOP    10 LOAD_ConST    0 (None)  13 RETURN_VALUE>>> def not__in():    not ('ham' in 'spam and eggs')>>> dis.dis(not__in)  20 LOAD_ConST    1 ('ham')   3 LOAD_ConST    2 ('spam and eggs')   6 COMPARE_OP    7 (not in)   9 POP_TOP    10 LOAD_ConST    0 (None)  13 RETURN_VALUE>>> def noteq():    not 'ham' == 'spam and eggs'>>> dis.dis(noteq)  20 LOAD_ConST    1 ('ham')   3 LOAD_ConST    2 ('spam and eggs')   6 COMPARE_OP    2 (==)   9 UNARY_NOT  10 POP_TOP    11 LOAD_ConST    0 (None)  14 RETURN_VALUE

起初我以为它们总是给出相同的结果,但是

not
它本身只是一个低优先级的逻辑否定运算符,可以
a in b
像其他布尔表达式一样容易地应用,而
notin
为了方便和清楚起见,它是一个单独的运算符。

上面的拆卸很明显!看起来,虽然

not
显然是逻辑取反运算符,但该表格
not a in b
是特殊情况,因此实际上并未使用通用运算符。这
not a inb
实际上使表达式与相同
a not in b
,而不仅仅是产生相同值的表达式。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5650109.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存