In my last blog post, I tried to understand how Python counts items in a sequence.
This week P2PU’s Mookie gave us to read on functions and amongst them the id()
function, which shows the unique ID where that object is stored in memory.
Immediately I went and tested how last week’s theory works with this. And again I found it very interesting and also a bit odd.
First I tried what happens if I create two seemingly unrelated variables and assign them the same string.
>>> s = "MONTY"
>>> v = "MONTY"
>>> id(v)
140601249401952
>>> id(s)
140601249401952
It seems Python is smart enough that it assigns the same string to just one address in memory. I assume this way it conserves memory. Which is pretty cool.
And cooler still, that the same goes for items in the sequence.
>>> id(s[0])
140601249649048
>>> id(v[0])
140601249649048
>>> id(s[1])
140601250016472
>>> id(v[1])
140601250016472
>>> id(s[2])
140601250016432
>>> id(v[2])
140601250016432
>>> id(s[3])
140601250016832
>>> id(v[3])
140601250016832
>>> id(s[4])
140601249650768
>>> id(v[4])
140601249650768
>>> id(v[5])
Traceback (most recent call last):
File "", line 1, in
IndexError: string index out of range
>>> id(s[5])
Traceback (most recent call last):
File "", line 1, in
IndexError: string index out of range
And it also works backwards:
>>> id(s[-1])
140601249650768
>>> id(v[-1])
140601249650768
>>> id(s[-2])
140601250016832
>>> id(v[-2])
140601250016832
But does it also work with non-identical sequences that just happen to have some items identical?
>>> animals = ["dog", "sheep", "bird", "python"]
>>> mammals = ["dog", "sheep", "cow"]
>>> id(animals)
140601249224392
>>> id(mammals)
140601249258816
>>> id(mammals[0])
140601249407984
>>> id(animals[0])
140601249407984
>>> id(animals[2])
140601249402096
>>> id(mammals[2])
140601249407464
Yes it does! As you can see the ID of the two lists are different, but since in both the first item is "dog"
, that’s stored in the same place in memory. Of course the third item, which is not identical, has a different ID again.
So far so good, but again I have a question: Why the ID (and hence address in memory, I assume) of the sequence and the first item in it is so different?
hook out → playing a bit more with Python, and then back to boring legal books