Is empty string cached?

Bryan Olson fakeaddress at nowhere.org
Wed Feb 15 17:58:11 EST 2006


Farshid Lashkari wrote:
> 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,'')


In this case, the language implementation may either create new
strings or re-use existing ones:

     for immutable types, operations that compute new values
     may actually return a reference to any existing object with
     the same type and value, while for mutable objects this is
     not allowed.
     [http://docs.python.org/ref/objects.html]


[...]
> 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?

Either; see the same reference page.


-- 
--Bryan



More information about the Python-list mailing list