Is empty string cached?
Steve Holden
steve at holdenweb.com
Wed Feb 15 21:43:01 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,'')
>
> 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?
>
It takes far too little evidence to induce belief:
>>> a = "hello"
>>> b = "h"+"ello"
>>> a is b
False
>>> c = "hello"
>>> b is a
False
>>>
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
More information about the Python-list
mailing list