[Python-Dev] Re: Caching objects in memory

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Apr 26 06:37:33 CEST 2005


Guido van Rossum wrote:

> But for *immutable* objects (like numbers, strings and tuples) the
> implementation is free to use caching. In practice, I believe ints
> between -5 and 100 are cached, and 1-character strings are often
> cached (but not always).

Also, string literals that resemble Python identifiers
are often interned, although this is not guaranteed.
And this only applies to literals, not strings constructed
dynamically by the program (unless you explicitly apply
intern() to them).

Python 2.3.4 (#1, Jun 30 2004, 16:47:37)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> "foo" is "foo"
True
 >>> "foo" is "f" + "oo"
False
 >>> "foo" is intern("f" + "oo")
True

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+


More information about the Python-Dev mailing list