"/a" is not "/a" ?

Gary Herron 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
>>>>         
> 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!
>   

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
True
 >>> 1001 is 1000+1
False

 >>> 10*'a' is 5*'aa'
True
 >>> 100*'a' is 50*'aa'
False


Gary Herron


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




More information about the Python-list mailing list