chunking a long string?

Chris Angelico rosuav at gmail.com
Sat Nov 9 10:30:16 EST 2013


On Sun, Nov 10, 2013 at 2:21 AM, Roy Smith <roy at panix.com> wrote:
> But, you missed the point of my question.  You said that Python does
> this "only when you ask for it".  That implies it never interns strings
> if you don't ask for it, which is clearly not true:
>
> $ python
> Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
> [...]
>>>> x = "foo"
>>>> y = "foo"
>>>> x is y
> True

Ah! Yes, that's true; literals are interned - I forgot that. But
anything from an external source won't be, hence my example with
reading in the contents of a file.

> I think what you're trying to say is that there are several possible
> interning policies:
>
> 1) Strings are never interned
>
> 2) Strings are always interned
>
> 3) Strings are optionally interned, at the discretion of the
> implementation
>
> 4) The user may force a specific string to be interned by explicitly
> requesting it.
>
> and that Pike implements #1, while Python implements #3 and #4.

Pike implements #2, I presume that was a typo. And yes, the interning
of literals falls under #3, while sys.intern() gives #4. Use of #1
would be restricted to languages with mutable strings, I would expect,
for the same reason that Python tuples might be shared but lists won't
be.

ChrisA



More information about the Python-list mailing list