"/a" is not "/a" ?

Emanuele D'Arrigo manu3d at gmail.com
Fri Mar 6 16:42:57 EST 2009


On 6 Mar, 19:46, Gary Herron <gher... at 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



More information about the Python-list mailing list