18

Various Python guides say to use x is None instead of x == None. Why is that? Equality is used for comparing values, so it seems natural to ask if x has the value None, denoted with == and not is. Can someone explain why is is the preferred form and show an example where the two do not give the same answer?

Thanks.

1

3 Answers 3

14

The reason people use is is because there is no advantage to using ==. It is possible to write objects that compare equal to None, but it is uncommon.

class A(object):
    def __eq__(self, other):
        return True

print A() == None

Output:

True

The is operator is also faster, but I don't consider this fact important.

8
  • 3
    I guess you could also add that there is only ever one None object, and it has no value. Since all Nones are the same object, you should use the object identity operator is. The comparison operator == is inappropriate because None has no value.
    – Hamish
    Jun 23, 2012 at 4:29
  • 3
    @Hamish In what sense does it "have no value"? It is a value, just like every other object.
    – Marcin
    Jun 23, 2012 at 4:45
  • Why is the is operator faster? The reasoning for None being valueless makes no sense to me still... seems arbitrary. How is None different from False or other values? It's a value equal to None... by definition
    – user248237
    Jun 23, 2012 at 5:02
  • 5
    @user248237 the is operator is potentially faster because it can't be overloaded - there is no equivalent to the chain of calls that a == b potentially makes (usually: a.__eq__(b), b.__eq__(a) and finally a is b - first one to return something other than NotImplemented wins). is only tests one thing, that Python knows about directly, and always gives you an answer after one test. None being valueless is tenuous, but you could think of it like a null C pointer - a pointer's value is what it points to, and a null pointer points to nothing.
    – lvc
    Jun 23, 2012 at 5:51
  • 3
    @user248237 but the important thing is that is None and is not None say exactly what you mean - as DietrichEpp points out, it is possible that x == None can be overloaded to return True for a non-None x, possibly via a bug. is implies equality (usually, see float('nan') for the sole counter-example), but equality doesn't always imply identity even when comparing against singletons (you need the singleton on the LHS, to have its __eq__ do return self is other, and to not compare it against something that subclasses it). is says what you mean - much easier than rules.
    – lvc
    Jun 23, 2012 at 6:01
11

The is keyword tests identity. It is not a comparison operator like ==. Using is does more than test whether two arguments have the same value and/or the same internal structure: namely, it tests whether the two actually refer to the same object in memory. There are numerous implications to this, one of them being that is cannot be overloaded, and another being that behavior differs between mutable and immutable types. For example, consider the following:

>>> l1 = range(5)
>>> l2 = range(5)
>>> l1 == l2
True
>>> l1 is l2
False
>>> l3 = l1
>>> l1 is l3
True
>>> s1 = "abcde"
>>> s2 = "abcde"
>>> s1 == s2
True
>>> s1 is s2
True

Here, because lists are mutable, they cannot share a location in memory, and consequently is and == yield discrepant results. On the other hand, strings are immutable, and therefore their memory may be pooled in some cases. Basically, is can only reliably be used for simple, immutable types, or in instances when multiple names point to exactly the same object in memory (such as the use of l3 in the above example), and its use indicates a desire to test identity rather than value. I would expect is to be slightly faster than == because it doesn't perform method lookup, but I could be mistaken. Of course, for complex container objects like lists or dicts, is should be much faster than == (O(1) vs. O(n), presumably). That said, the speed issue is mostly a moot point, as the two should not be regarded as interchangeable.

10
  • 1
    is only tests memory location in CPython - in other implementations, it is perfectly free to test other things too (eg, PyPy treats ints specially, letting value determine identity). String interning isn't guaranteed either. You are right about is not doing method lookups, though - == can do two of them (one for each operand) before falling back to an identity check, and either of those can apply any kindof crazy equality rule it wants - including comparing equal to a singleton.
    – lvc
    Jun 23, 2012 at 6:07
  • @lvc, thanks for clarifying. Feel free to edit my post if it needs correcting. I don't claim to be an expert in CPython's internals, or in those of any other implementation.
    – Greg E.
    Jun 23, 2012 at 6:10
  • I wouldn't call scheme's eq? predicate very precise, since a lot of behavior is unspecified. Its behavior is less specified/precise than eqv?, which is in turn less specified/precise than equal?. Jun 23, 2012 at 9:10
  • @DietrichEpp, it's been a long while since I've done any coding in Scheme, but IIRC equal? does nothing more than apply eqv? to sequence/container types like vectors, cons cells, etc. In particular, equal? will return #t when comparing distinct objects, stored in different locations in memory, that are structurally identical. Clearly, that doesn't parallel the behavior of is. I seem to recall that eq? was the most primitive/low-level identity predicate, that much of its behavior in practice defaulted to that of eqv?, and that a lot of situations were implementation-dependent.
    – Greg E.
    Jun 23, 2012 at 9:37
  • @DietrichEpp, q.v., gnu.org/software/mit-scheme/documentation/mit-scheme-ref/…, which specifies that eq? only returns #t when eqv? would, and that eq? could be done as a simple pointer comparison (whether this is the practice in specific implementations, I have no idea). I was groping for some analogy to Python's is in my explanation and for reasons that escape comprehension, eq? is what come bubbling up from my subconscious. Of all the Scheme predicates, it seems closest, though whether any Scheme predicate is apposite is another matter.
    – Greg E.
    Jun 23, 2012 at 9:50
3

PEP 8 says: "Comparisons to singletons like None should always be done with 'is' or 'is not', never the equality operators." Here is a quite good explanation why:

http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.