is implemented with id ?

Oscar Benjamin oscar.j.benjamin at gmail.com
Sat Nov 3 21:14:29 EDT 2012


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).

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).


Oscar



More information about the Python-list mailing list