[Python-ideas] Keyword/Symbol literals

Chris Angelico rosuav at gmail.com
Wed Jan 21 21:58:18 CET 2015


On Thu, Jan 22, 2015 at 6:18 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> Why can't we make interned strings a subclass of strings like bool is a
> subclass of int?

My gut feeling is that an interned string is exactly the same thing as
a non-interned string, and that it should be possible for sys.intern()
to return the original string unchanged. But on the flip side, if you
*know* that two strings are interned, you can compare their identities
instead of their values; currently, identical interned strings compare
equal quickly, but distinct ones still have to be compared
character-for-character:

>>> timeit.repeat("x==y","x='a'*65536+'q'; y='a'*65536+'q'")
[2.535494999960065, 2.2198410001583397, 2.2210810002870858]
>>> timeit.repeat("x==y","import sys; x=sys.intern('a'*65536+'q'); y=sys.intern('a'*65536+'q')")
[0.056215000338852406, 0.05387900024652481, 0.04834900004789233]
>>> timeit.repeat("x==y","x='a'*65536+'x'; y='a'*65536+'y'")
[2.53101500030607, 2.2251750002615154, 2.228022000286728]
>>> timeit.repeat("x==y","import sys; x=sys.intern('a'*65536+'x'); y=sys.intern('a'*65536+'y')")
[2.583013000432402, 2.2194420001469553, 2.224679999984801]

If, in the fourth example, the interpreter knew that both those string
objects were interned, it could know that they can't possibly be the
same string. So is that distinction worth coding into the class
hierarchy?

ChrisA


More information about the Python-ideas mailing list