"is" and "=="

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Thu Mar 13 09:05:13 CET 2003


> From: Thomas Wouters [mailto:thomas at xs4all.net]
> 
> On Wed, Mar 12, 2003 at 02:57:08AM +0300, Alexander Semenov wrote:
> 
> > But in case of rare insertion and often comparing
> > one can use intern():
> 
> > >>> a = 'spam'
> > >>> b = 'sp'+'am'
> > >>> a is b
> > 0
> > >>> a == b
> > 1
> > >>> a = intern(a)
> > >>> b = intern(b)
> > >>> a is b
> > 1
> 
> 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.

[1] In 2.3+ intern() will return the same instance only so long as there is at least one reference to the interned string other than the interning map. Interned strings are now subject to the same reference counting behaviour as any other string. In earlier versions of CPython interned strings existed for the lifetime of the program. So, for the following piece of code:

    a = 'spam'
    b = 'sp'+'am'
    print a is b
    print a == b
    a = intern(a)
    b = intern(b)
    print a is b
    c = id(a)
    d = id(b)
    print c == d
    del a
    del b
    print c == id(intern("spam"))

versions prior to 2.3 will output

    0
    1
    1
    1
    1

and 2.3+ *may* output

    False
    True
    True
    True
    False

The 2.3 behaviour is generally much more useful - prior to 2.3 people tended not to use interned strings precisely because they use up memory for the entire lifetime of the program.

Tim Delaney





More information about the Python-list mailing list