The ABCs of Python: The Identity of “is”

is

On how to use the Python keyword is

In English, no verb is more profound than “to be”.  All the really deep questions and statements contain some form of is or am.

  • Who am I?
  • What are we?
  • Is there a God?
  • I think, therefore I am.
  • To be or not to be

In Python, the keyword is isn’t quite that deep, but it’s definitely a reflection of its more profound cousin.

Python’s is can be used to test the identity of objects.  But, you may ask, isn’t that what the == symbol does in Python, as in 3 == 3? Well,  not quite. Just because my clone and I are equivalent doesn’t make us the same person. Similarly, an empty list is equivalent to another empty list, but they are not the exact same list. Either one of of them may change at any moment, becoming more than they are. Therefore:

>>> [] == []
True
>>> [] is []
False
>>> {} == {}
True
>>> {} is {}
False

Yes, there are also good technical reasons for this.  Lists and dictionaries are located separately in Python’s memory.

Get a load of the code below. Which statement is going to print? By now, you probably know, but you might want to run it and see for sure:

a = [1, 2, 3]
b = [1, 2, 3]
if a is b:
    print("a really is b")
else:
    print("a and b are equal but not the same")

The bottom line is that is compares the identity of two Python objects whereas == compares their values. Honestly, I’m not sure if that’s really deep or if I’ve just been dipping too deeply into my Zeppelin collection this evening.

For more on how to use “is,” see GeeksforGeeks.

Featured image: Hamlet’s Vision by Pedro Américo

Published by

Mark R. Vickers

I am a writer, analyst, futurist and researcher. I've spent most of my working life as an editor and manager for research organizations focusing on social, business, technology, HR and management trends. But, perhaps more to the point for this blog, I'm curious about the universe and the myriad, often mysterious relationships therein.

2 thoughts on “The ABCs of Python: The Identity of “is””

Leave a Reply