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

Alan Gauld alan.gauld at yahoo.co.uk
Thu May 27 07:44:42 EDT 2021


On 27/05/2021 02:49, boB Stepp wrote:
> I often wonder how Python design decisions are reached.  I wish there
> was a single go-to place for these sorts of questions!
> 
Technically this is an implementation decision, I don;t think
the Python language requires small ints to be cached, its
a purely CPython design choice and could change.

> I cannot resist asking why -- if anyone happens to know -- more
> positive integers are cached than negative ones:

I'd guess because positive ints are used way more often than
negative ones. I very rarely use a negative number literal
or even perform operations where I expect to get a negative
result. Index searches and limits and slices etc are all
specified by positive numbers except for a very few
negatives used for accessing end points(-1 thru' -5 at most!)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list