0

Why does python store all the numbers up to 256 or 2^8 at static points in memory?

>>> id(2**8) == id(2**8), id(2**8+1) == id(2**8+1)
(True, False)
Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
  • CPython interpreter optimization. – Klaus D. Sep 19 '19 at 15:29
  • 2
    Possible duplicate of ["is" operator behaves unexpectedly with integers](https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers) – pault Sep 19 '19 at 15:34
  • 3
    From the [docs](https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong): *The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.* – pault Sep 19 '19 at 15:35

0 Answers0