0

enter image description here

When i check the id(10) twice(Object of type int),the interpreter gives the same result result. But when i try the same with the tuple object (1,2,3), I am getting different answer despite the fact that both int and tuple are immutable. Can i know the reason why this is happening?

AEM
  • 1,354
  • 8
  • 20
  • 30

1 Answers1

1

ints have a very special behavior in Python, until 257, no new objects are assigned. Take this:

>>> a, b = 257, 257
>>> id(a)
140640774013296
>>> id(b)
140640774013296

But when you do:

>>> a = 258
>>> b = 258
>>> id(a)
140410944685744
>>> id(b)
140410944685872

Note that the ids are different, the same doesn't apply to tuples where a new object is created each time.

RMPR
  • 3,368
  • 4
  • 19
  • 31