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
The is keyword is used to test if two variables refer to the same object. The test returns True if the two objects are the same object. The test returns False if they are not the same object, even if the two objects are 100% equal. Use the == operator to test if two variables are equal.
Yes, two or more references, say from parameters and/or local variables and/or instance variables and/or static variables can all reference the same object.
For reference type like objects, == or === operators check its reference only. here a==b will be false as reference of both variables are different though their content are same. and if i check now a==b then it will be true , since reference of both variable are same now.
== operator is used to check whether two variables reference objects with the same value.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With