2

I am fairly ok with Python, but recently I learned something that made me question everything I thought to be true in my life :D

So if I run this:

w1 = "word"
w2 = "word"

print(id(w1))
print(id(w2))

To my utter horror, the output is:

140675515277936
140675515277936

I always believed that as strings are immutable, they will have their own address. I was wrong. My question then:

How does the Python Memory Manager find existing string (or any) objects when a new variable is created, so it can make that new variable point to them?

With some hashing? If besides your answer you can point me to learning material about this, i will be double grateful! Cheers!

user3435407
  • 1,019
  • 4
  • 15
  • 31
  • 1
    You must have run this as a script, rather than line-by-line. Within a single compilation, Python will notice that equal constants have been used, and combine them into one for efficiency. This is *not something you should ever care about*, as none of the defined semantics of strings are affected by whether equal strings are represented by the same object or not. – jasonharper May 12 '22 at 19:49
  • I see, so this is just the compiler making execution easier and it probably uses indeed a hashing mechanism. Thanks! – user3435407 May 12 '22 at 19:58
  • 1
    "I always believed that as strings are immutable, they will have their own address" that's actually not what immutability implies at all. In fact, quite the opposite. Immutability generally means *you are free to re-use objects* without a fear of bad consequences. – juanpa.arrivillaga May 12 '22 at 20:00
  • 1
    @jasonharper note, although what you are describing is certainly true, it might be interesting to note that the Python runtime will intern strings in other situations too. But again, this is something that one should probably never worry about – juanpa.arrivillaga May 12 '22 at 20:01
  • Thanks juanpa, what I mean by that, is that strings have their own identity, they are sort of independent entities, hence they need their own space of existence. So when I copy them (x = y), the number of entities increases. Where as with a list for example, it would be just a renaming, or giving it another name. The number of existing entities don't change. At least this is how I conceptualized it. But I see what you mean, I learned from it, thank you! – user3435407 May 12 '22 at 20:05
  • 1
    You might find these posts rather interesting: [are strings cached](https://stackoverflow.com/questions/42684966/are-strings-cached) and [string comparison speed](https://stackoverflow.com/a/71644988/6340496); as they cover other areas of this topic to help provide a rounded context. – S3DEV May 12 '22 at 20:19

0 Answers0