views:

545

answers:

5

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?

Thanks!

+10  A: 

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

Ignacio Vazquez-Abrams
Ah interesting! In which situations might one want to fake the equality comparison btw? I'm guessing this has security implications in some way.
viksit
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.
Thomas Wouters
I would say it's more *confusion* implications than security implications.
Greg Hewgill
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.
Ignacio Vazquez-Abrams
Ah that way, I see. Thx for the clarification.
viksit
+28  A: 

== 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.

Thomas Wouters
Thanks for the answer - could you elaborate on situations when an object can break, being compared to None?
viksit
@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`.
S.Lott
Equality vs. identity: got it! This hints of social commentary to me!
jathanism
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.
MatrixFrog
@MatrixFrog. Thats a good one. Didnt know the difference between is / =, but now I know i'll remember it. (And probably will spread the word, using the .equals example)
Tom
+8  A: 

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)
Alok
+5  A: 
>>> () 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.

ephemient
Most of these only work by coincidence/implementation detail. `()` and `1` are not inherently singletons.
Mike Graham
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.
ephemient
It is how it is implemented, but it isn't meaningful or useful or educational.
Mike Graham
+5  A: 

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 !=.

Wesley