性能没有差异,因为它们可以编译为相同的字节码:
>>> 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话就没有歧义。


![Python`if x is not None`或`if not is is None`?[关闭] Python`if x is not None`或`if not is is None`?[关闭]](http://www.mshxw.com/aiimages/31/669447.png)
