6

Try this in an interactive python shell.

[] is [ ]

The above returns False, why?

mtt2p
  • 1,818
  • 1
  • 15
  • 22
Jonathan
  • 10,792
  • 5
  • 65
  • 85

4 Answers4

17

You created two mutable objects, then used is to see if those are the same object. That should definitely return False, or something would be broken.

You wouldn't ever want is to return true here. Imagine if you did this:

foo = []
bar = []
foo.append(42)

then you'd be very surprised if bar now contains 42. If is returned true, meaning that both [] invocations returned the exact same object, then appending to foo would be visible in the reference to bar.

For immutable objects, it makes sense to cache objects, at which point is may return true, like with empty tuples:

>>> () is ()  # are these two things the same object?
True

The CPython implementation has optimised empty tuple creation; you'll always get the exact same object, because that saves memory and makes certain operations faster. Because tuples are immutable, this is entirely safe.

If you expected to test for value equality instead, then you got the wrong operator. Use the == operator instead:

>>> [] == []  # do these two objects have the same value?
True
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

In python is does a reference equality check like [] and [] they are different objects you can check that by

print id([]),id([])

or

 In [1]: id([])
Out[1]: 140464629086976

In [2]: id([])
Out[2]: 140464628521656

both will return different address and both are different object so is will always give false

[] is []

output

false
Anand Tripathi
  • 14,556
  • 1
  • 47
  • 52
  • 3
    Those `id` tests aren't doing what you think they are. It's quite common for the `print` to print the same number twice, because the lists don't have overlapping lifetimes. Similarly, this is why your `==` test with `id` values gave True instead of False. – user2357112 Mar 02 '17 at 20:45
0

[] is like list(), if you do this:

a = list()
b = list()

clearly a and b are two completly different objects, hence:

a is b # False

like

list() is list() # False

like

[] is [] # False
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Netwave
  • 40,134
  • 6
  • 50
  • 93
  • 1
    Why does it matter what way the empty list objects were created? – Martijn Pieters Mar 02 '17 at 10:54
  • @MartijnPieters, it does not matter, maybe i did not explained well, or you did not understand what i would like to explain? – Netwave Mar 02 '17 at 10:57
  • What I mean is that it doesn't matter if you use `[] is []` or `list() is list()`; both expressions create empty list objects. To say that *`[]` is like `list()`* you are not explaining anything about why the `is` test returns `False`, because the difference between `[]` and `list()` doesn't matter here. Also, your `== False` tests are incorrect; you are testing if `(a is b) and (b == False)`, because Python comparison operators are *chained*. – Martijn Pieters Mar 02 '17 at 11:45
  • @MartijnPieters, ok so i explained badly, i was just poiting that `[]` and `list()` both build objects, and in each call both build diferent objects, so it was obvious that `[] is []` would be evaluated to false. About the operators, it was just me relaxing and writting python sintax for making the point, not written reliable python code, sorry. – Netwave Mar 02 '17 at 12:11
0

The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not.

id('') : 139634828889200
id('') : 139634828889200
id('') : 139634828889200

id([]) : 139634689473416
id([]) : 139634689054536
id([]) : 139634742570824
barbsan
  • 3,418
  • 11
  • 21
  • 28