Can someone explain this to me? So I\'ve been playing with the id() command in python and came across this:
>>> id(\'cat\')
5181152
>>> a = \'c
The code you posted creates new strings as intermediate objects. These created strings eventually have the same contents as your originals. In the intermediate time period, they do not exactly match the original, and must be kept at a distinct address.
>>> id('cat')
5181152
As others have answered, by issuing these instructions, you cause the Python VM to create a string object containing the string "cat". This string object is cached and is at address 5181152.
>>> a = 'cat'
>>> id(a)
5181152
Again, a has been assigned to refer to this cached string object at 5181152, containing "cat".
>>> a = a[0:2]
>>> id(a)
27731511
At this point in my modified version of your program, you have created two small string objects: 'cat'
and 'ca'
. 'cat'
still exists in the cache. The string to which a
refers is a different and probably novel string object, containing the characters 'ca'
.
>>> a = a + 't'
>>> id(a)
39964224
Now you have created another new string object. This object is the concatenation of the string 'ca'
at address 27731511, and the string 't'
. This concatenation does match the previously-cached string 'cat'
. Python does not automatically detect this case. As kindall indicated, you can force the search with the intern()
method.
Hopefully this explanation illuminates the steps by which the address of a
changed.
Your code did not include the intermediate state with a
assigned the string 'ca'
. The answer still applies, because the Python interpreter does generate a new string object to hold the intermediate result a[0:2]
, whether you assign that intermediate result to a variable or not.