On 6 Mar, 19:46, Gary Herron <gher...@islandtraining.com> wrote:
> It is an implementation choice (usually driven by efficiency considerations) 
> to choose when two strings with the same value are stored in memory once or 
> twice.  In order for Python to recognize when a newly created string has the 
> same value as an already existing string, and so use the already existing 
> value, it would need to search *every* existing string whenever a new string 
> is created.  Clearly that's not going to be efficient.  However, the C 
> implementation of Python does a limited version of such a thing -- at least 
> with strings of length 1.

Gary, thanks for your reply: your explanation does pretty much answer
my question. One thing I can add however is that it really seems that
non-alphanumeric characters such as the forward slash make the
difference, not just the number of characters. I.e.

>>> a = "aaaaaaaaaaaaaaaaaaaaaaaaaaa"
>>> b = "aaaaaaaaaaaaaaaaaaaaaaaaaaa"
>>> a is b
True
>>> a = "/aaaaaaaaaaaaaaaaaaaaaaaaaaa"
>>> b = "/aaaaaaaaaaaaaaaaaaaaaaaaaaa"
>>> a is b
False

I just find it peculiar more than a nuisance, but I'll go to the
blackboard and write 100 times "never compare the identities of two
immutables". Thank you all!

Manu
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to