chunking a long string?

Chris Angelico rosuav at gmail.com
Sat Nov 9 17:14:28 EST 2013


On Sun, Nov 10, 2013 at 2:37 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> 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").

I'm pretty sure it's simply that literals are interned, or at least
shared across a module (and the interactive interpreter "counts" as a
module). And it might still only be ones which look like identifiers,
because:

>>> foo = "lorem ipsum dolor sit amet"
>>> bar = "lorem ipsum dolor sit amet"
>>> foo is bar
False

My "only" was false because of the sharing/interning of (some)
literals, which I'd forgotten about; however, there's still the
distinction that I was trying to draw, that in Python _some strings_
are interned (a feature you can explicitly request), rather than _all
strings_ being interned. And as is typical of python-list, it's this
extremely minor point that became the new course of the thread - my
main point was not whether all, some, or no strings get interned, but
that string interning makes the storage space of duplicate strings
immaterial :)

ChrisA



More information about the Python-list mailing list