Python: Difference between is and equals (==)
== operator
checks the values behind the two names. It returns True if the
two values are equal, False otherwise.
is operator
checks the objects behind the two names. It returns True if
both the names refer to the same object, False otherwise.
There’s a difference in meaning between equal and identical. And this difference
is important when you want to understand how Python’s is and == comparison
operators behave.
The == operator compares by
checking for equality.
The is operator, however,
compares identities.
Example1:
First, we’ll create a new list object and
name it a, and then define another variable b that points to the
same list object:
a = [1, 2, 3]
b = a
print(a)
print(b)
Let’s inspect these two variables. We can see
they point to identical looking lists:
Output:
[1, 2, 3]
[1, 2, 3]
Because the two list objects look the same
we’ll get the expected result when we compare them for equality using the == operator:
>>> a == b
True
Note: They both have same ids also.
>>> a = [1, 2, 3]
>>> b = a
>>> id(a)
4319913352
>>> id(b)
4319913352
>>>
>>>
However, that doesn’t tell us whether a and b are
actually pointing to the same object (We can check with id() function).
Of course, we know they do because we
assigned them earlier, but suppose we didn’t know—how might we find out?
The answer is comparing both variables with
the is operator. This confirms both variables are in fact pointing to
one list object:
>>> a is b
True
Let’s see what happens when we create an
identical copy of our list object. We can do that by calling list() on
the existing list to create a copy we’ll name c:
c = list(a)
Again you’ll see that the new list we just
created looks identical to the list object pointed to by a and b:
>>> c
[1, 2, 3]
Now this is where it gets interesting—let’s
compare our list copy c with the initial list a using
the == operator. What answer do you expect to see?
>>> a == c
True
Okay, I hope this was what you expected. What
this result tells us is that c and a have the same contents.
They’re considered equal by Python. But are
they actually pointing to the same object? Let’s find out with the is operator:
>>> a is c
False
>>> id(a)
4319913352
>>>
>>> id(c)
4319937160
>>>
This is where we get a different result.
Python is telling us that c and a are pointing to two
different objects, even though their contents might be the same.
So, to recap let’s try and break the
difference between is and == down to two short definitions:
- An is expression
evaluates to True if two variables point to the same (identical)
object.
- An == expression
evaluates to True if the objects referred to by the variables
are equal (have the same contents).
Example2:
>>> a = b = [1,2,3]
>>> c = [1,2,3]
>>> a == b
True
>>> a == c
True
>>> a is b
True
>>> a is c
False
>>> a = [1,2,3]
>>> b = [1,2]
>>> a == b
False
>>> a is b
False
>>> del a[2]
>>> a == b
True
>>> a is b
False
NOTE: Avoid using is operator for immutable types
such as strings and numbers, the result is unpredictable.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.