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?
Asked
Active
Viewed 73 times
0

AEM
- 1,354
- 8
- 20
- 30

Kishan Teja
- 31
- 4
-
Don't post text as image – Marcin Orlowski Apr 18 '20 at 18:09
1 Answers
1
int
s 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