How does the Python Memory Manager find existing objects?

Viewed 26

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!

0 Answers
Related