Comparisons in Python: the difference between “is” and “==”
Comparing one thing to another is constant in programming. But Python has two ways to do this:
is. So what’s the difference, anyway?
Let’s give ourselves a class to work with as we see how this all works. A cat, obviously.
def __init__(self, name):
self.name = name def __eq__(self, other):
return self.name == other.name
You’ll notice we’ve defined an
__eq__ method on our
Cat class: this lets us give a…