Python != operation vs "is not"

254,423

Solution 1

== is an equality test. It checks whether the right hand side and the left hand side are equal objects (according to their __eq__ or __cmp__ methods.)

is is an identity test. It checks whether the right hand side and the left hand side are the very same object. No methodcalls are done, objects can't influence the is operation.

You use is (and is not) for singletons, like None, where you don't care about objects that might want to pretend to be None or where you want to protect against objects breaking when being compared against None.

Solution 2

First, let me go over a few terms. If you just want your question answered, scroll down to "Answering your question".

Definitions

Object identity: When you create an object, you can assign it to a variable. You can then also assign it to another variable. And another.

>>> button = Button()
>>> cancel = button
>>> close = button
>>> dismiss = button
>>> print(cancel is close)
True

In this case, cancel, close, and dismiss all refer to the same object in memory. You only created one Button object, and all three variables refer to this one object. We say that cancel, close, and dismiss all refer to identical objects; that is, they refer to one single object.

Object equality: When you compare two objects, you usually don't care that it refers to the exact same object in memory. With object equality, you can define your own rules for how two objects compare. When you write if a == b:, you are essentially saying if a.__eq__(b):. This lets you define a __eq__ method on a so that you can use your own comparison logic.

Rationale for equality comparisons

Rationale: Two objects have the exact same data, but are not identical. (They are not the same object in memory.) Example: Strings

>>> greeting = "It's a beautiful day in the neighbourhood."
>>> a = unicode(greeting)
>>> b = unicode(greeting)
>>> a is b
False
>>> a == b
True

Note: I use unicode strings here because Python is smart enough to reuse regular strings without creating new ones in memory.

Here, I have two unicode strings, a and b. They have the exact same content, but they are not the same object in memory. However, when we compare them, we want them to compare equal. What's happening here is that the unicode object has implemented the __eq__ method.

class unicode(object):
    # ...

    def __eq__(self, other):
        if len(self) != len(other):
            return False

        for i, j in zip(self, other):
            if i != j:
                return False

        return True

Note: __eq__ on unicode is definitely implemented more efficiently than this.

Rationale: Two objects have different data, but are considered the same object if some key data is the same. Example: Most types of model data

>>> import datetime
>>> a = Monitor()
>>> a.make = "Dell"
>>> a.model = "E770s"
>>> a.owner = "Bob Jones"
>>> a.warranty_expiration = datetime.date(2030, 12, 31)
>>> b = Monitor()
>>> b.make = "Dell"
>>> b.model = "E770s"
>>> b.owner = "Sam Johnson"
>>> b.warranty_expiration = datetime.date(2005, 8, 22)
>>> a is b
False
>>> a == b
True

Here, I have two Dell monitors, a and b. They have the same make and model. However, they neither have the same data nor are the same object in memory. However, when we compare them, we want them to compare equal. What's happening here is that the Monitor object implemented the __eq__ method.

class Monitor(object):
    # ...

    def __eq__(self, other):
        return self.make == other.make and self.model == other.model

Answering your question

When comparing to None, always use is not. None is a singleton in Python - there is only ever one instance of it in memory.

By comparing identity, this can be performed very quickly. Python checks whether the object you're referring to has the same memory address as the global None object - a very, very fast comparison of two numbers.

By comparing equality, Python has to look up whether your object has an __eq__ method. If it does not, it examines each superclass looking for an __eq__ method. If it finds one, Python calls it. This is especially bad if the __eq__ method is slow and doesn't immediately return when it notices that the other object is None.

Did you not implement __eq__? Then Python will probably find the __eq__ method on object and use that instead - which just checks for object identity anyway.

When comparing most other things in Python, you will be using !=.

Solution 3

Consider the following:

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

c = Bad()
c is None # False, equivalent to id(c) == id(None)
c == None # True, equivalent to c.__eq__(None)

Solution 4

None is a singleton, therefore identity comparison will always work, whereas an object can fake the equality comparison via .__eq__().

Solution 5

>>> () is ()
True
>>> 1 is 1
True
>>> (1,) == (1,)
True
>>> (1,) is (1,)
False
>>> a = (1,)
>>> b = a
>>> a is b
True

Some objects are singletons, and thus is with them is equivalent to ==. Most are not.

Share:
254,423
viksit
Author by

viksit

The most common mistake people make when designing something completely foolproof, is underestimating the ingenuity of complete fools. - Douglas Adams, Mostly Harmless

Updated on June 23, 2020

Comments

  • viksit
    viksit almost 4 years

    In a comment on this question, I saw a statement that recommended using

    result is not None
    

    vs

    result != None
    

    I was wondering what the difference is, and why one might be recommended over the other?

  • viksit
    viksit about 14 years
    Ah interesting! In which situations might one want to fake the equality comparison btw? I'm guessing this has security implications in some way.
  • Thomas Wouters
    Thomas Wouters about 14 years
    It's not about faking equality, it's about implementing equality. There are lots of reasons to want to define how an object compares to another.
  • Greg Hewgill
    Greg Hewgill about 14 years
    I would say it's more confusion implications than security implications.
  • viksit
    viksit about 14 years
    Thanks for the answer - could you elaborate on situations when an object can break, being compared to None?
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 14 years
    I have not come up against a reason to fake equality against None, but incorrect behavior regarding None could occur as a side effect of implementing equality against other types. It's not so much security implications as it is just correctness implications.
  • user1066101
    user1066101 about 14 years
    @viksit. None has few methods and almost no attributes. If your __eq__ test expected a method or attribute, it might break. def __eq__( self, other ): return self.size == other.size. For example, will break if other happens to be None.
  • Mike Graham
    Mike Graham about 14 years
    Most of these only work by coincidence/implementation detail. () and 1 are not inherently singletons.
  • ephemient
    ephemient about 14 years
    In the CPython implementation, the small integers (-NSMALLNEGINTS <= n <= NSMALLPOSINTS) and empty tuples are singletons. Indeed it's not documented nor guaranteed, but it's unlikely to change.
  • Tyler
    Tyler about 14 years
    My favorite way to comprehend this is: Python's is is like Java's ==. Python's == is like Java's .equals(). Of course this only helps if you know Java.
  • Mike Graham
    Mike Graham about 14 years
    It is how it is implemented, but it isn't meaningful or useful or educational.
  • me_and
    me_and over 10 years
    And in particular, CPython is not the only Python implementation. Relying on behaviour that can vary across Python implementations would seem to generally be a Bad Idea™ to me.
  • Orwellophile
    Orwellophile about 7 years
    @MatrixFrog: In PHP or JavaScript we would say that is is like === (very equal), and conversely is not is like !== (not exactly equal).
  • Asad Moosvi
    Asad Moosvi over 6 years
    Is is not a single operator or is it just negating the result of is internally like not foo is bar?
  • FriskySaga
    FriskySaga over 2 years
    Great answer! To add on, an example of an object that would be break when being compared to None via an equality test would be the dateutil.relativedelta.relativedelta object. When compared to != None, it would return False, and when compared to == None, it would also return False. Weird, I know.