Is empty string cached?

Steve Holden steve at holdenweb.com
Wed Feb 15 22:38:07 EST 2006


Farshid Lashkari wrote:
>>It takes far too little evidence to induce belief:
>>
>> >>> a = "hello"
>> >>> b = "h"+"ello"
>> >>> a is b
>>False
>> >>> c = "hello"
>> >>> b is a
>>False
>> >>>
> 
> 
> I don't understand the point of your last expression. Were you intending 
> this instead:
> 
>  >>> c is a
> True
> 
Yes.

> However, the following commands add to my confusion:
> 
>  >> a = 'wtf?'
>  >> b = 'wtf?'
>  >> a is b
> False
> 
> So how are string literals cached? Is there an explanation somewhere? Is 
> it some freaky voodoo, and I should just assume that a string literal 
> will always generate a new object?
> 
I really don't understand why it's so important: it's not a part of the 
language definition at all, and therefore whatever behavior you see is 
simply an artifact of the implementation you observe.

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