“is” operator result: What is happening?

I was quite surprised when

[] is not []

evaluated to True.

What is happening in this code? What really not and is statements are doing?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

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

Method 4

is means is same instance. It evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

Reference, near the bottom.

Method 5

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.

Method 6

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

Method 7

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x