I have a case where I would like to defeat string interning.
Let's say the string I have is "foo bar"
.
I know of a few hacky/not obvious ways to defeat interning a string. All involve computing the expression at runtime.
In [1]: my_str = "foo bar"
In [2]: my_new_str1 = " ".join(["foo", "bar"])
In [3]: my_new_str2 = "foo bar "[:-1]
In [4]: my_new_str3 = "foo " + "bar"
In [5]: id(my_str)
Out[5]: 4483028144
In [6]: id(my_new_str1)
Out[6]: 4483030192
In [7]: id(my_new_str2)
Out[7]: 4484125872
In [8]: id(my_new_str3)
Out[8]: 4484052336
There is a built-in function sys.intern
, which interns a string. I am looking to do the exact opposite, not intern something in a simple/descriptive way.
Is there anything out there that can defeat string interning in a "clean" way?
You could also subclass str
.
>>> class UninternedStr(str):
... pass
...
>>> s = UninternedStr('a string')
>>> s1 = UninternedStr('a string')
>>> s is s1
False