1

I was reading through the answer of Slicing a list in Python without generating a copy. The answer uses

>>> a = [1000 + 1, 1000 + 1, 1000 + 1]
>>> map(id, a)
[140502922988976, 140502922988952, 140502922988928]

to demonstrate that each integer gets a unique ID in a.

As a sanity check, I ran the following code in my own environment (I'm on python3 so had to call list on the map)

> a = [1+1, 1+1, 1+1]
> list(map(id,a))
[140228695033632, 140228695033632, 140228695033632]

All I changed was the 1000 to 1 and now python assigns the same id to each number! I also ran the original code with 1000, which gave three different IDs. Why is the behavior different for different sized integers?

I also wanted to play around with some other immutables.

Tuples always get different IDs.

> a = [(1,), (1,), (1,)]
> list(map(id,a))
[140228631126592, 140228631112728, 140228631128944]

Simple strings get the same ID.

> a = ['a', 'a', 'a']
> list(map(id,a))
[140068125361128, 140068125361128, 140068125361128]

Longer strings get different IDs.

> a = ['a'*1000, 'a'*1000, 'a'*1000]
> list(map(id,a))
[17589280, 17579408, 17580480]

I also investigated if it was something specific to lists:

> a, b = 1+1, 1+1
> a is b
True
> a, b = 1000+1, 1000+1
> a is b
False

What's going on here?

David Hou
  • 39
  • 3
  • 1
    integers in range -5 to 256 or so are cached for performance reasons and have the same id. changing 1000+1 to 1+1 brought you into the cache-area. See/google "string interning" for strings. See [c.PyLong docu](https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong) – Patrick Artner Jan 29 '19 at 19:46
  • 2
    The TL;DR is that this is an implementation detail in the reference python interpreter. Don't count on either one always being the case. People usually hit this for the first time when trying to understand the difference between `==` and `is`, so you can get some more info if you find some of those questions. – glibdud Jan 29 '19 at 19:48
  • 1
    Do not rely on this oddity, which immutable types Python will decide to _cache_ is not easily predictable and may vary from implementation to implementation. Read more at [3.1. Objects, values and types](https://docs.python.org/3/reference/datamodel.html#objects-values-and-types). – zwer Jan 29 '19 at 19:51

0 Answers0