[Tutor] Why does CPython cache small integers "asymmetrically"?

boB Stepp robertvstepp at gmail.com
Sat May 29 11:10:30 EDT 2021


This is in response to Peter's response.  I see that in my original
post I had an error in my terminal session capture (Of course!).

On Wed, May 26, 2021 at 8:49 PM boB Stepp <robertvstepp at gmail.com> wrote:

> >>> x = -4
> >>> y = -4
> >>> id(x)
> 1176492075152
> >>> id(y)
> 1176492075152
> >>> x = -5
> >>> x = -5  # OOPS!  That was supposed to be "y = -5"!
> >>> id(x)
> 1176492075120
> >>> id(y)
So of course y here is referring to the last set value of -4 and is
different from that of -5.

Thanks for catching that Peter!  I spent quite some time trying to
parse the source Cameron cited looking for a problem that was actually
on my end.  I confirm Peter's results:

PS C:\Users\boB> py
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = -4
>>> y = -4
>>> id(x)
2238155155600
>>> id(y)
2238155155600
>>> x = -5
>>> y = -5
>>> id(x)
2238155155568
>>> id(y)
2238155155568
>>> x = -6
>>> y = -6
>>> id(x)
2238164080720
>>> id(y)
2238164080944

boB Stepp


More information about the Tutor mailing list