String Identity Test

S Arrowsmith sion at paintbox.UUCP
Wed Mar 4 16:15:09 CET 2009

Avetis KAZARIAN  <avetis.k at> wrote:
>It seems that any strict ASCII alpha-numeric string is instantiated as
>an unique object, like a "singleton" ( a =3D "x" and b =3D "x" =3D> a is b =
>and that any non strict ASCII alpha-numeric string is instantiated as
>a new object every time with a new id.

What no-one appears to have mentioned so far is that the purpose
of this implementation detail is to ensure that there is a single
instance of strings which are valid identifiers, so that you don't
go around creating and destroying string instances just to do an
attribute look-up on an object. A few strings which are not valid
as identifiers get swept up into this system:

>>> a = "1"
>>> b = "1"
>>> a is b

"Small" integers get a similar treatment:

>>> a = 256
>>> b = 256
>>> a is b
>>> a = 257
>>> b = 257
>>> a is b

But as as hopefully been made clear, all this is completely an
implementation detail. (Indeed, the range of "interned" integers
changed from 0--99 to -5--2356 a few versions ago.) So don't,
under any circumstances, rely on it, even when you understand
what's going on.


   under construction

More information about the Python-list mailing list