Alex Rivera | Logout

Python != operation vs "is not"

Asked 2010-02-05T19:21:45.577
355

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

result is not None

vs

result != None

What is the difference? And why might one be recommended over the other?

Edit
Report

1 Answer

50

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)
answered 2010-02-05T20:03:36.450

Your Answer