is implemented with id ?

Chris Angelico rosuav at gmail.com
Sat Nov 3 23:19:55 EDT 2012


On Sun, Nov 4, 2012 at 2:10 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> /* Shortcut for empty or interned objects */
> if (v == u) {
>     Py_DECREF(u);
>     Py_DECREF(v);
>     return 0;
> }
> result = unicode_compare(u, v);
>
> where v and u are pointers to the unicode object.

There's a shortcut if they're the same. There's no shortcut if they're
both interned and have different pointers, which is a guarantee that
they're distinct strings. They'll still be compared char-for-char
until there's a difference.

But it probably isn't enough of a performance penalty to be concerned
with. It's enough to technically prove the point that 'is' is faster
than '==' and is still safe if both strings are interned; it's not
enough to make 'is' better than '==', except in very specific
situations.

ChrisA



More information about the Python-list mailing list