性能没有差异,因为它们可以编译为相同的字节码:
>>> 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话就没有歧义。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)