Simplest way to defeat string interning in Python

230 Views Asked by At

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?

2

There are 2 best solutions below

0
Silvio Mayolo On BEST ANSWER

You could also subclass str.

>>> class UninternedStr(str):
...   pass
... 
>>> s = UninternedStr('a string')
>>> s1 = UninternedStr('a string')
>>> s is s1
False
2
dawg On

I suppose put a function around join:

def new_str(s):
    return ''.join(s)

>>> s='a string'

>>> s1=new_str(s)

>>> id(s)
4568404208

>>> id(s1)
4569455664

>>> s is s1
False

But I think this is silly...