0

Look at the following code:

a = [1, 2, 3]
# a is a reference to an object of type 'list'
b = [1, 2, 3]
# b is a reference to an object of type 'list'
print(a is b)

Output: False

a = (1, 2, 3)
# a is a reference to an object of type 'tuple'
b = (1, 2, 3)
# b is a reference to an object of type 'tuple'
print(a is b)

Output: True

Why is the output different in both cases? Has a mutable or an immutable structure got something to do with it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Niraj Raut
  • 205
  • 2
  • 7
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/213129/discussion-on-question-by-niraj-raut-why-is-the-output-different-in-both-the-cas). – Samuel Liew May 05 '20 at 01:19

4 Answers4

0

if a and b are pointing to same address then a is b gives true even thought they are not pointing to same address if values are same then a==b gives true

naveen
  • 23
  • 1
  • 5
  • How does this address the OP's question? They're not asking what `is` does, they're asking why the behaviour is different between the two examples. – jonrsharpe May 04 '20 at 13:31
0

'is' operator – Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. if this explains!?

This should help

  • How does this address the OP's question? They're not asking what `is` does, they're asking why the behaviour is different between the two examples. – jonrsharpe May 04 '20 at 13:29
  • That is explained in the other thread already, please check the link. – Kunal Sikri May 04 '20 at 13:39
  • 1
    Then this isn't really an answer, and the question should be closed as a duplicate. – jonrsharpe May 04 '20 at 13:40
  • I don't have privilege to do that, so I pointed out the link and summary. Edit: Did It. – Kunal Sikri May 04 '20 at 13:41
  • 1
    Then please wait [until you do](https://stackoverflow.com/help/privileges/comment), that doesn't excuse posting non-answers. See e.g. https://meta.stackexchange.com/q/225370/248731. – jonrsharpe May 04 '20 at 13:43
  • If you go through tons of questions here a lot of them have been pointed to other links. I learned and did that way. But I did post summarized answer. – Kunal Sikri May 04 '20 at 13:44
  • 1
    That's unfortunate, but at least now you know better than to keep making that mistake! – jonrsharpe May 04 '20 at 13:45
0

is compares the IDs (which are the memory addresses in CPython), == compares equality.

a = [1, 2, 3]
# a is a reference to an object of type 'list'
b = [1, 2, 3]
# b is a reference to an object of type 'list'
print(id(a), id(b))
print(hex(id(a)), hex(id(b)))
print(a is b)


a = (1, 2, 3)
# a is a reference to an object of type 'tuple'
b = (1, 2, 3)
# b is a reference to an object of type 'tuple'
print(hex(id(a)), hex(id(b)))
print(id(a), id(b))
print(a is b)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Jortega
  • 3,616
  • 1
  • 18
  • 21
  • How does this address the OP's question? They're not asking what `is` does, they're asking why the behaviour is different between the two examples. – jonrsharpe May 04 '20 at 13:29
  • @jonrsharpe They want to know why one gives an output of true and the other false. Understanding what `is` does answers the why one gives true and the other false. The additional print statements show what `is` is comparing. – Jortega May 04 '20 at 13:40
  • ...does it? It would be perfectly reasonable to have a Python implementation that works exactly as described here where `a is not b` in *both* cases. As I mentioned above, the question isn't about the implementation of `is`, but why tuples get reused. – jonrsharpe May 04 '20 at 13:41
  • His question "Why is the output different in both cases?" the answer is because the `id` values are different on one case and the same in the other case. – Jortega May 04 '20 at 13:43
  • Sure, but... *why* are they the same IDs in the second case but not the first? They both look to be creating separate objects. That's what your answer fails to address, and what the OP is actually asking. – jonrsharpe May 04 '20 at 13:44
  • @jonrsharpe, the owner asked "Why is the output different in both cases?" Not "why are they the same IDs in the second case but not the first". The answer to that question is that the python memory manager is pointing both variables to the same address in memory for the immutable object and different objects in memory for the mutable object. – Jortega May 04 '20 at 13:53
  • ...what? I know that isn't what they asked, but *that's what your answer tells them* - that's my point! You don't actually explicitly say that the ID is the memory address, and this is *also* implementation-dependent. And you don't explain how and why a given implementation might reuse the underlying tuple object in the second case. This is a very surface-level answer, they're identical because they're the same object, but doesn't actually explain the reason. – jonrsharpe May 04 '20 at 13:56
  • @jonrsharpe ok, I edited the answer to explicitly say: "'is' compares the IDs which are the memory addresses" – Jortega May 04 '20 at 14:03
  • Again, that's a [CPython implementation detail](https://docs.python.org/3/library/functions.html#id), and it still doesn't address the actual question - why do they have the same memory address, **why are they the same object?** – jonrsharpe May 04 '20 at 14:05
-2

it is false and it is same for both lists and tupless

if a=[1,2,3]

and b=a then a is b gives true

if a=[1,2,3] and b=[1,2,3] then a is b gives true

it is same for both lists and tuples

naveen
  • 23
  • 1
  • 5
  • when we are writing a=b it is pointing to same address so a is b gives true – naveen May 04 '20 at 14:20
  • It is same both for lists and tuples .The question is wrong . – naveen May 04 '20 at 14:26
  • You have got a different output because you are working on a different implementation. It's completely implementation-defined. My question is correct. – Niraj Raut May 04 '20 at 14:28
  • It isn't *wrong*, it's *implementation dependent*. For example, I see the tuples being identical if I put this in a `.py` file and run it, but *not* if I do the same example in the REPL, even for the same version of CPython 3. – jonrsharpe May 04 '20 at 14:29