1

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?

5
  • 4
    Please say why? Commented May 19, 2021 at 2:21
  • 1
    Why would you need to avoid string interning? strings are immutable which means once you have string, no one can change it Commented May 19, 2021 at 2:22
  • Long story short, I want is checks to return False. So if I said spam = "hi" and ham = "hi", I want spam is ham to return False. Commented May 19, 2021 at 2:33
  • 1
    This answer has the most direct low level copy. Commented May 19, 2021 at 2:37
  • 1
    One reason you might want this would be if you want to tell the difference between a default value and an equivalent value that was passed in. For instance, using the argparse module, you can set a default argument value there is no direct way to determine if the value came from the default or was specified explicitly. Commented Oct 22, 2023 at 16:48

2 Answers 2

3

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...

Sign up to request clarification or add additional context in comments.

2 Comments

Yes thank you @dawg for the answer. In the OP, I already included .join as one method. I was wondering if there's a more descriptive/meaningful way. I would prefer not to add a utility function new_str to my client code, even though it's not many lines of code
Descriptive and meaningful are in the eye of the beholder. What is descriptive and meaningful to you?
2

You could also subclass str.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.