Compare if two variables reference the same object in python

Python

Python Problem Overview


How to check whether two variables reference the same object?

x = ['a', 'b', 'c']
y = x                 # x and y reference the same object
z = ['a', 'b', 'c']   # x and z reference different objects

Python Solutions


Solution 1 - Python

That’s what is is for.

In the example, x is y returns True because it is the same object while x is z returns False because it are different objects (which happen to hold identical data).

Solution 2 - Python

While the two correct solutions x is z and id(x) == id(z) have already been posted, I want to point out an implementation detail of python. Python stores integers as objects, as an optimization it generates a bunch of small integers at its start (-5 to 256) and points EVERY variable holding an integer with a small value to these preinitialized objects. More Info

This means that for integer objects initialized to the same small numbers (-5 to 256) checking if two objects are the same will return true (ON C-Pyhon, as far as I am aware this is an implementation detail), while for larger numbers this only returns true if one object is initialized form the other.

> i = 13
> j = 13
> i is j
True

> a = 280
> b = 280
> a is b
False

> a = b
> a
280
> a is b
True


Solution 3 - Python

y is x will be True, y is z will be False.

Solution 4 - Python

You can also use id() to check which unique object each variable name refers to.

In [1]: x1, x2 = 'foo', 'foo'

In [2]: x1 == x2
Out[2]: True

In [3]: id(x1), id(x2)
Out[3]: (4509849040, 4509849040)

In [4]: x2 = 'foobar'[0:3]

In [5]: x2
Out[5]: 'foo'

In [6]: x1 == x2
Out[6]: True

In [7]: x1 is x2
Out[7]: False

In [8]: id(x1), id(x2)
Out[8]: (4509849040, 4526514944)

Solution 5 - Python

I really like to have a visual feedback, that's why I sometimes just open up http://www.pythontutor.com/visualize.html#mode=edit to see how the memory is allocated and what is referencing what.

enter image description here

Added this awesome gif as this reply is about visualizing..

Solution 6 - Python

This is from docs.python.org: "Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity."

Apparently every time you change the value the object is recreated as indicated by the identity changing. The line x=3 followed by the line x=3.14 gives no error & gives different identities, types and values for x.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpic11View Question on Stackoverflow
Solution 1 - PythonJochen RitzelView Answer on Stackoverflow
Solution 2 - PythontedView Answer on Stackoverflow
Solution 3 - PythonMark RushakoffView Answer on Stackoverflow
Solution 4 - PythonBillView Answer on Stackoverflow
Solution 5 - Pythonuser1767754View Answer on Stackoverflow
Solution 6 - PythonScott WoodsView Answer on Stackoverflow