[Tutor] Python cashes low integers? How? Where?

Dick Moores rdm at rcblue.com
Mon Aug 9 00:32:28 CEST 2004


I've been reading Wesley Chun's _Core Python Programming_ today, about 
object identities. Using p. 85 I entered

 >>> a = 4
 >>> b = 1 + 3
 >>> a is b
True

The book says this will be False. The book's errata page 
(<http://starship.python.net/crew/wesc/cpp/errata.htm>) says that the 
error is because Python cashes low integers, and to try this with floats:

 >>> a = 4.0
 >>> b = 1.0 + 3.0
 >>> a is b
False

I tested with larger integers:

 >>> f = 100
 >>> g = 99 + 1
 >>> f is g
False

OK. Now, this intrigued me. Where does Python do this cashing? Not in my 
computer's memory, because rebooting doesn't change the above results. So 
where?

Thanks,

Dick Moores
Win XP, Python 2.3.4



More information about the Tutor mailing list