When does python choose to intern a string [duplicate]

筅森魡賤 提交于 2019-11-26 12:17:57

问题


This question already has an answer here:

  • About the changing id of an immutable string 5 answers
>>> 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.


回答1:


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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/10622472/when-does-python-choose-to-intern-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!