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

Cameron Simpson cs at cskk.id.au
Wed May 26 23:37:52 EDT 2021


On 26May2021 20:49, boB Stepp <robertvstepp at gmail.com> wrote:
>I often wonder how Python design decisions are reached.  I wish there
>was a single go-to place for these sorts of questions!
>
>I cannot resist asking why -- if anyone happens to know -- more
>positive integers are cached than negative ones:

Probably it has been judged that there are more commonly used small 
positive integers used than negative ones, maybe from some statistics (I 
can imagine that this is benchmarkable if your test cases can be 
considered representative).

The constants themselves seem to be here:

    https://github.com/python/cpython/blob/3e7ee02327db13e4337374597cdc4458ecb9e3ad/Include/internal/pycore_interp.h#L211

I have no insight into how these were chosen though.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list