-1

When I call for the id() of variables x and y they return the same value, and this makes sense to me, since both x and y are assigned to 10. But why isn't the same thing happening to list_1 and list_2 since they both are assigned to [1,2,3,4,5]?

>>> x = 10
>>> y = 10
>>> id(x)
140715221221936
>>> id(y)
140715221221936
>>> list_1 = [1,2,3,4,5]
>>> list_2 = [1,2,3,4,5]
>>> id(list_1)
2808959614408
>>> id(list_2)
2808959614152
  • That `id(x) == id(y)` is true should actually be surprising. `id` provides for *identity* not *equality*. `list_1` and `list_2` are *two different lists*. The way your wrote `x` and `y` would reasonably lead you to expect that they are *different ints* , but Python, as an implementation detail, caches small ints, `[-5, 257]` IIRC. Try it with 1000. Note, this is an *implementation detail*, the runtime is free to optimize these sorts of things for immutable objects, but you should never rely on this behavior – juanpa.arrivillaga Aug 06 '20 at 21:28
  • Thanks @juanpa.arrivillaga, I am new at both python and stackOverflow. I apricciate your help, it was enlightening . – Gustavo Nóbrega Aug 06 '20 at 23:50
  • No worries. Just remember, the most important thing here is *that this is an implementation detail*. Your original instinct that " x = 10 , y = 10 - > id(x) and id(y) would be different," is *correct*. The above bevahior is an optimization, and the fact that these objects are the same shouldn't be relied on, so don't try to be clever and write code liek `if some_function(value) is 0:`, because *you should never use identity to test for equality* despite these optimizations – juanpa.arrivillaga Aug 07 '20 at 00:07

1 Answers1

1

Integers are immutable, so Python keeps a cache of small integers that it uses. Because the objects are cached they always have the same ID.

Lists are mutable, it would be very unfortunate if changing list_2 messed up list_1 by accident. So they can't be shared unless you do it yourself with list_2 = list_1.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • I thought that if the code was: x = 10 , y = 10 - > id(x) and id(y) would be different, they would only be the same if the code was: x = 10 , y = x, then -> id(x) would be the same as id(y) – Gustavo Nóbrega Aug 06 '20 at 21:09
  • @GustavoNóbrega yes, precisely, that is what you should think, but as an implementation detail / optimization, small integers [5, 257] are cached by the Python runtime (although there are edge cases where that isn't true). Note, the interpreter will also cache literal constants of immutable objects if they are compiled in the same block, so in a repl, `x = 1000` and newline, `y = 1000`, the id's will be different, but `x = 1000; y = 1000` and the ids the same. – juanpa.arrivillaga Aug 06 '20 at 21:30
  • @GustavoNóbrega it's an optimization specially for small integers, since the [Pareto Principle](https://en.wikipedia.org/wiki/Pareto_principle) means those will be the most commonly used. – Mark Ransom Aug 06 '20 at 21:30
  • Thanks @MarkRansom for your help, I am a beginner, so thank you for taking the time. – Gustavo Nóbrega Aug 06 '20 at 23:50