chunking a long string?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Nov 9 10:37:03 EST 2013


On Sat, 09 Nov 2013 09:37:54 -0500, Roy Smith wrote:

> In article <mailman.2283.1383985583.18130.python-list at python.org>,
>  Chris Angelico <rosuav at gmail.com> wrote:
> 
>> Some languages [intern] automatically for all strings, others (like
>> Python) only when you ask for it.
> 
> What does "only when you ask for it" mean?

In Python 2:

help(intern)


In Python 3:

import sys
help(sys.intern)


for more info. I think that Chris is wrong about Python "only" interning 
strings if you explicitly ask for it. I recall that Python will (may?) 
automatically intern strings which look like identifiers (e.g. "spam" but 
not "Hello World" or "123abc"). Let's see now:

# using Python 3.1 on Linux

py> s = "spam"
py> t = "spam"
py> s is t
True

but:

py> z = ''.join(["sp", "am"])
py> z is s
False

However:

py> u = "123abc"
py> v = "123abc"
py> u is v
True

Hmmm, obviously the rules are a tad more complicated than I thought... in 
any case, you shouldn't rely on automatic interning since it is an 
implementation dependent optimization and will probably change without 
notice.



-- 
Steven



More information about the Python-list mailing list