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: ==
and 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.
class Cat():
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 custom definition for what it means for two objects to be equal (docs here). In this case, we’ve said that if two cats have the same name, they equal each other.
Ok, let’s instantiate some instances of cats, Mena and Llewyn, and another cat, also named Mena, but not my cat Mena, and compare them:
>>> mena = Cat("Mena")
>>> llewyn = Cat("Llewyn")
>>> another_cat_named_mena = Cat("Mena")>>> mena == llewyn
False
>>> mena == another_cat_named_mena
True
This aligns with what we’d expect, given how we defined equality on Cat
above: mena
and another_cat_named_mena
have the same name, so we’ve decided they’re equal. But they’re not actually the same cat. Let’s prove it! Every object in Python is assigned a unique id
, which actually references its location in memory, though that’s not important for our purposes:
>>> id(mena)
4427700624
>>> id(llewyn)
4427700880
>>> id(another_cat_named_mena)
4427701008
This confirms that mena
and another_cat_named_mena
are actually different cats. So what if we want to know whether two objects we have are actually the same instance of an object, even with an overridden __eq__
method ? That’s where is
comes in! Let’s take a look:
>>> mena == another_cat_named_mena
True # we already knew this!
>> mena is another_cat_named_mena
False
is
under the hood actually compares the id
of the object, and only returns True
if they’re the same instance. While we can customize how ==
is evaluated, Python does not allow for is
to be customized.
Built-ins
When it comes to built-ins, including types and constants, even when we instantiate…