"/a" is not "/a" ?
gherron at islandtraining.com
Sat Mar 7 00:09:23 CET 2009
Emanuele D'Arrigo wrote:
> 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
>>>> a = "/aaaaaaaaaaaaaaaaaaaaaaaaaaa"
>>>> b = "/aaaaaaaaaaaaaaaaaaaaaaaaaaa"
>>>> a is b
> 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!
Unless you are *trying* to discern something about the implementation
and its attempt at efficiencies. Here's several more interesting example:
>>> 101 is 100+1
>>> 1001 is 1000+1
>>> 10*'a' is 5*'aa'
>>> 100*'a' is 50*'aa'
More information about the Python-list