I've always thought of the
if not x is None
if x is not None
...to compare singletons like
None. Use is or is not.
There's no performance difference, as they compile to the same bytecode:
Python 2.6.2 (r262:71600, Apr 15 2009, 07:20:39) >>> import dis >>> def f(x): ... return x is not None ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (x) 3 LOAD_CONST 0 (None) 6 COMPARE_OP 9 (is not) 9 RETURN_VALUE >>> def g(x): ... return not x is None ... >>> dis.dis(g) 2 0 LOAD_FAST 0 (x) 3 LOAD_CONST 0 (None) 6 COMPARE_OP 9 (is not) 9 RETURN_VALUE
Stylistically, I try to avoid
not x is y. Although the compiler will always treat it as
not (x is y), a human reader might misunderstand the construct as
(not x) is y. If I write
x is not y then there is no ambiguity.