Hi, I'm Harlin and welcome to my blog. I write about Python, Alfresco and other cheesy comestibles.

Python - All Things Being Equal: "is" vs "=="

In the world of drink branding it is very important that all of your products of the same line of drinks taste exactly the same. Companies like Coca Cola, Pepsi, Budweiser and all the rest go through great lengths to make sure that their drinks taste exactly the same. This shouldn't matter if the drink is produced in different parts of the United States or sometimes in different parts of the world.

If you drink a Coke bottled in New York and drink one from California, you'll notice they taste exactly the same. To test equality, Python allows one to use "==" to test for values and "is" to test for identity. I'll give an example of what this means.

Here we can compare two lists:

>>> my_list = [9, 5, 1]

Now, let's create another list (my_other_list) which is a copy of my_list:

>>> my_other_list = list(my_list)

Now, we can compare their values to each other:

>>> my_list == my_other_list
True

Ok, this makes sense. If our "==" comparison evaluates to True, then the values of lists will be equal. If we drink two Budweisers, they will taste exactly the same.

As mentioned, there is another comparison operator called "is". Let's test our lists using "is":

>>> my_other_list is my_list
False

As you've probably guessed, we are getting a False when comparing the two. This is because we are comparing two separate objects to test if my_list is in fact the same object in memory called my_other_list. We may have identical looking bottles of beer but they are physically not the same thing.

We can test these two objects in more detail and find that they are not the same by using the id() function to get the memory address of each object:

>>> id(my_list)
139951594118536

>>> id(my_other_list)
139951594143880

Physically speaking, this makes sense because we are drinking two different bottles of beer. So, besides the obvious fact that they are in two different bottles, it's very likely that each bottle would have a different serial number.

Unlike reality (at least for now), when it comes to software, we can actually create two objects that are the same object. How do we do that? Well, let's have a look at this:

>>> my_list = [5, 3, 1]

This time instead of using list() to make a copy, we just assign my_other_list to be equal to my_list:

>>> my_other_list = my_list

So, let's test this out:

>>> my_list == my_other_list
True
>>> my_other_list is my_list
True

Ok, so far so good. Again, the lists are shown to have the same values. But, what about testing with id() function?

>>> id(my_list)
139951594116232
>>> id(my_other_list)
139951594116232

How about that? They are the same object.

So, how does this work exactly?

It's simple. The variable, my_other_list is simply a pointer to the object in memory that my_list is. In our first example, we just made a copy of my_list and so then both my_list and my_other_list were pointing to two different memory addresses. Now that we've used the "=" operator to assign my_other_list to be my_list, we create a pointer to the original memory address.

We can further prove this by changing my_list to include a new number, "0":

>>> my_list.append(0)
>>> my_list
[5, 3, 1, 0]

By printing out the value of my_other_list, we can see that my_other_list has the same values without explicitly adding to it and we can prove that they both are pointing to the same object and same memory address:

>>> my_other_list
[5, 3, 1, 0]

We can't do this yet with soda or beer or any other thing, but we can do it with Python.

So now, you should understand the difference between comparing variables with "==" and "is".

I would also recommend this article which goes into a little more detail.

Any Comments, Always Welcome!