x and y are two variables.
I can check if they're equal using x == y
, but how can I check if they have the same identity?
Example:
x = [1, 2, 3]
y = [1, 2, 3]
Now x == y
is True because x and y are equal, however, x and y aren't the same object.
I'm looking for something like sameObject(x, y)
which in that case is supposed to be False.
To build on the answer from Mark Byers:
The
is
evaluation will work when the variables contain objects and not primitive types.If you need to comparte primitives as well, I'd suggest using the builtin
id()
function.From the Python docs:
You can use
is
to check if two objects have the same identity.