Is empty string cached?

Farshid Lashkari lashkariNO at SPAMworldviz.com
Wed Feb 15 16:18:36 EST 2006


When I pass an empty string to a function is a new string object created 
or does python use some global pre-created object? I know python does 
this with integer objects under a certain value. For instance, in the 
following code is a new string object created for each function call?

func(0,'')
func(1,'')
func(2,'')
func(3,'')

I tried the following commands in the interactive shell:

 >> x = ''
 >> y = ''
 >> x is y
True
 >> x = 'hello'
 >> y = 'hello'
 >> x is y
True

This leads me to believe that python does reuse existing strings, but 
once the variables are removed, does the item still exist in the cache?

-Farshid



More information about the Python-list mailing list