I was quite surprised when
[] is not []
evaluated to True
.
What is happening in this code? What really not
and is
statements are doing?
a is not b
is a special operator which is equivalent to not a is b
.
The operator a is b
returns True if a and b are bound to the same object, otherwise False. When you create two empty lists you get two different objects, so is
returns False (and therefore is not
returns True).
is
is the identity comparison.
==
is the equality comparison.
Your statement is making two different lists and checking if they are the same instance, which they are not. If you use ==
it will return true and because they are both empty lists.
The best way to describe WHY that happens is this:
Here is your example
>>> x = []
>>> y = []
>>> print(x is y)
... False
x
and y
are actually two different lists, so if you add something to x
, it does not appear in y
>>> x.append(1)
>>> print(x)
... [1]
>>> print(y)
... []
So how do we make (x is y
) evaluate true?
>>> x = []
>>> y = x
>>> print(x is y)
... True
>>> x.append(10)
>>> print(x)
... [10]
>>> print(y)
... [10]
>>> print(x is y)
... True
if you want to see if two lists have the same contents...
>>> x = []
>>> y = []
>>> print(x == y)
... True
>>> x.append(21)
>>> print(x)
... [21]
>>> print(y)
... []
>>> print(x == y)
... False
>>> y = [21]
>>> print(x == y)
... True
is checks for identity. []
and []
are two different (but equivalent) lists. If you want to check if both the lists are empty you can use their truth value (false for empty strings, collections, and zeros).
if not ([] and []):
print 'Spanish Inquisition'
the only time that is
is guaranteed to return True is for singletons such as None. Like the Highlander, there can be only one instance of None in your program - every time you return None it's the very same "thing" as the none referred to if you type print None
.
[], OTOH, is not guaranteed to be anything except an empty list and evaluate to False in a boolean context.
I know I am posting to a pretty old post. however, this might help someone stumble upon this like me.
"is" checks, whether a memory address is same or not, while "==" check if the value is same or not. Would be much clear from the following example
let's first talk about immutable objects, as it's easy to understand
# could be any immutable object
immutable_a = 10
immutable_b = 10
# prints address of a and b variable
print "address of a is %s" % id(immutable_a)
print "address of a is %s" % id(immutable_b)
# as both addresses is same, following shall be true
print immutable_a is immutable_b
# as the values are also same, following shall be true as well
print immutable_a == immutable_b
now let's talk about mutable objects
# could be any mutable object
mutable_a = [10]
mutable_b = [10]
# prints address of a and b variable
print "address of mutable_a is %s" % id(mutable_a)
print "address of mutable_b is %s" % id(mutable_b)
# as addresses are not same, following shall be false
print mutable_a is mutable_b
# as the values are same, following shall be true
print mutable_a == mutable_b
@Jiaaro is right. Using is
with immutable data types is dangerous because it is not predictable because of Pythons interpreter optimization.
See this example:
10 * "a" is 10 * "a" # True
100 * "a" is 100 * "a" # False
In the second line it is faster to create a new object with a new id for the interpreter. So use the is
operator only with mutable types.