What determines which strings are interned and when? [duplicate]
Asked Answered
S

3

14
>>> s1 = "spam"
>>> s2 = "spam"
>>> s1 is s2
True
>>> q = 'asdalksdjfla;ksdjf;laksdjfals;kdfjasl;fjasdf'
>>> r = 'asdalksdjfla;ksdjf;laksdjfals;kdfjasl;fjasdf'
>>> q is r
False

How many characters should have to s1 is s2 give False? Where is limit? i.e. I am asking how long a string has to be before python starts making separate copies of it.

Spurn answered 16/5, 2012 at 16:11 Comment(8)
Are you asking how long a string has to be before python starts making separate copies of it?Buller
It's completely implementation dependent and could change in the next version of Python without warning. Why do you want to know?Cracker
Yes, I am asking how long a string has to be before python starts making separate copies of it.Spurn
@Cris: What are you trying to accomplish? The behavior is implementation-defined and cannot be relied on. Why not just check for equality?Wigwag
I just want know for knowing. Don't know that this is no important and very flexible.Spurn
if you just want to know, why don't you test it on the implementation you're using?Benedick
if i will only testing i will never get knowledge that this is implementation dependentSpurn
Note that you may get a different result if you put your example statements in a script, instead of trying them interactively.Crucial
G
14

String interning is implementation specific and shouldn't be relied upon, use equality testing if you want to check two strings are identical.

Georgeta answered 16/5, 2012 at 16:15 Comment(4)
...implementation specific. This is also answer. Thank You.Spurn
@Cris As a concrete example, ideone's python version seems to intern your "long" strings as well as 1000-character-eval-constructed strings.Shunt
It gives me little more knowlednge and many more questions, byt it is ok.Spurn
So is there never a legit use case for users to test s1 is s2 ?Cassaundracassava
I
12

If you want, for some bizarre reason, to force the comparison to be true then use the intern function:

>>> a = intern('12345678012345678901234567890qazwsxedcrfvtgbyhnujmikolp')
>>> b = intern('12345678012345678901234567890qazwsxedcrfvtgbyhnujmikolp')
>>> a is b
True
Invalidate answered 16/5, 2012 at 16:19 Comment(6)
For some bizarre reason, good to know.Spurn
Additionally, intern is moved into module sys in python 3000 along with id. Also good to know.Invalidate
For some reasons, the most. Could You tell is intern() only python 3000 toy?Spurn
@Cris intern() has been in Python at least since version 1.5Shunt
intern is in the root namespace.Invalidate
It is built-in, you don't need to import anything.Mussel
S
6

Here is a piece of comment about interned string from CPython 2.5.0 source file (stringobject.h)

/* ... ... This is generally restricted to strings that **"look like" Python identifiers**, although the intern() builtin can be used to force interning of any string ... ... */

Accordingly, strings contain only underscores, digits or alphabets will be interned. In your example, q and ``r contain ;, so they will not be interned.

Streptothricin answered 9/5, 2014 at 4:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.