1

Apologies for a question that seems very basic/trivial but I don't understand the following behaviours:

>>> id_1 = id(256)
>>> id_2 = id(256)
>>> id_1 == id_2
True
>>> id_1 = id(257)
>>> id_2 = id(257)
>>> id_1 == id_2
False

>>> id_1 = id("ab")
>>> id_2 = id("ab")
>>> id_1 == id_2
True
>>> id_1 = id("aa")
>>> id_2 = id("aa")
>>> id_1 == id_2
False
>>> id_1 = id('  ')
>>> id_2 = id('  ')
>>> id_1 == id_2
False

>>> id_1 = id(('A', 'Z'))
>>> id_2 = id(('A', 'Z'))
>>> id_1 == id_2
True
>>> id_1 = id(('A', 'Z', 'AA'))
>>> id_2 = id(('A', 'Z', 'AA'))
>>> id_1 == id_2
False

What is the difference between the special cases which always give the same object reference/address given by the builtin function id (The once I saw are 0-256, both inclusive, 'a'-'z', 'A'-'Z')?

0xc0de
  • 8,028
  • 5
  • 49
  • 75
  • 2
    Also see [About the changing id of a Python immutable string](http://stackoverflow.com/q/24245324). You also create objects that are *destroyed immediately after accessing their `id()`*, so the memory address is freed and the `id()` value can be *reused*. – Martijn Pieters Oct 30 '14 at 08:09

0 Answers0