Fix Python – Compare if two variables reference the same object in python

Question

Asked By – pic11

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

Now we will see solution for issue: Compare if two variables reference the same object in python


Answer

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).

This question is answered By – Jochen Ritzel

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0