As I know, immutability of object in python means that when we modify this object, new object being created in computer memory and new object's id and old_object's id are different. Then I can't understand, why when I create my string variable s = 'Dragon'
and then adding a char, id is changing. But when I keep adding characters to string s
, id is not changing. Why?
s = 'Dragon'
print(id(s))
s += 'a'
print(id(s), s)
s += 'b'
print(id(s), s)
s += 'c'
print(id(s), s)
140019146112112
140019146112240 Dragona
140019146112240 Dragonab
140019146112240 Dragonabc