interning strings

Peter Otten __peter__ at web.de
Sun Nov 7 16:09:39 CET 2004


Mike Thompson <none.by.e-mail> wrote:

> The interning of strings has me puzzled.  Its seems to happen sometimes,
> but not others. I can't decern the pattern and I can't seem to find
> documentation regarding it.

Strings of length < 2 are always interned:
>>> a = ""
>>> a is ""
True
>>> a = " "
>>> a is " "
True
>>> "aname"[1] is "n"
True

String constants that are potential attribute names are also interned:
>>> a = "could_be_a_name"
>>> a is "could_be_a_name"
True
>>> a = "could not be a name"
>>> a is "could not be a name"
False

...although the algorithm to determine whether a string constant could be a
name is simplistic (see all_name_chars() in compile.c, or believe that it
does what its name suggests):
>>> a = "1x"
>>> a is "1x"
True

Strings that are otherwise created are not interned:
>>> a = "aname"
>>> a is "a" + "name"
False

By the way, why would you want to mess with these implementation details?
Use the == operator to compare strings and be happy ever after :-)

Peter




More information about the Python-list mailing list