Python`if x is not None`或`if not is is None`?[关闭]

Python`if x is not None`或`if not is is None`?[关闭],第1张

Python`if x is not None`或`if not is is None`?[关闭]

性能没有差异,因为它们可以编译为相同的字节码:

>>> import dis>>> dis.dis("not x is None")  10 LOAD_NAME     0 (x)   2 LOAD_ConST    0 (None)   4 COMPARE_OP    9 (is not)   6 RETURN_VALUE>>> dis.dis("x is not None")  10 LOAD_NAME     0 (x)   2 LOAD_ConST    0 (None)   4 COMPARE_OP    9 (is not)   6 RETURN_VALUE

从风格上讲,我尝试避免

not x is y
,人类读者可能会误以为
(not x) is y
。如果我写的
x is not y
话就没有歧义



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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存