"is" and "=="

Thomas Wouters thomas at xs4all.net
Thu Mar 13 04:40:22 EST 2003


On Thu, Mar 13, 2003 at 07:05:13PM +1100, Delaney, Timothy C (Timothy) wrote:
> > From: Thomas Wouters [mailto:thomas at xs4all.net]

> > One can, but it would still be better to use '==', as this works even
> > when intern (possible, in the future) would fail to do its job, and the
> > identity test is the first test == does.

> There is a guarantee that intern() will always[1] return the same string
> instance for strings that compare equal. So you can rely on this
> behaviour. It is *not* possible for intern to fail to do its job except
> via a bug.

Not at this time, no, hence my remark 'possible, in the future'. We started
deprecating 'apply', so there is no reason to assume 'intern' will never go
away. However, my post was wrong for a different reason: the identity check
is no longer done in the fast path of ==, starting at Python 2.1 (from the
looks of it.) It's still in the fast path of string comparison though.

So, while 'is' being true no longer means '==' is true, I still believe, in
general, it's better coding style to use '==' rather than 'is', even if you
know you have intern'ed strings. It isn't *that* much slower than
string-comparing interned strings, and it could save you from embarassing
bugs where you forgot to intern a string ;)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!





More information about the Python-list mailing list