__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 ""
>>> a = " "
>>> a is " "
>>> "aname" is "n"
String constants that are potential attribute names are also interned:
>>> a = "could_be_a_name"
>>> a is "could_be_a_name"
>>> a = "could not be a name"
>>> a is "could not be a name"
...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"
Strings that are otherwise created are not interned:
>>> a = "aname"
>>> a is "a" + "name"
By the way, why would you want to mess with these implementation details?
Use the == operator to compare strings and be happy ever after :-)
More information about the Python-list