is implemented with id ?

Chris Angelico rosuav at gmail.com
Sat Nov 3 21:22:17 EDT 2012


On Sun, Nov 4, 2012 at 12:14 PM, Oscar Benjamin
<oscar.j.benjamin at gmail.com> wrote:
> On 3 November 2012 22:50, Chris Angelico <rosuav at gmail.com> wrote:
>> This one I haven't checked the source for, but ISTR discussions on
>> this list about comparison of two unequal interned strings not being
>> optimized, so they'll end up being compared char-for-char. Using 'is'
>> guarantees that the check stops with identity. This may or may not be
>> significant, and as you say, defending against an uninterned string
>> slipping through is potentially critical.
>
> The source is here (and it shows what you suggest):
> http://hg.python.org/cpython/file/6c639a1ff53d/Objects/unicodeobject.c#l6128
>
> Comparing strings char for char is really not that big a deal though.
> This has been discussed before: you don't need to compare very many
> characters to conclude that strings are unequal (if I remember
> correctly you were part of that discussion).

Yes, and a quite wide-ranging discussion it was too! What color did we
end up whitewashing that bikeshed? *whistles innocently*

> I can imagine cases where I might consider using intern on lots of
> strings to speed up comparisons but I would have to be involved in
> some seriously heavy and obscure string processing problem before I
> considered using 'is' to compare those interned strings. That is
> confusing to anyone who reads the code, prone to bugs and unlikely to
> achieve the desired outcome of speeding things up (noticeably).

Good point. It's still true that 'is' will be faster, it's just not worth it.

ChrisA



More information about the Python-list mailing list