I know there are tons of resources out there about pointers and references (or rather: names, and bindings!) in Python, but I am struggling to understand one last point:
I get that if a = 1
and if b = 1
than they both are 'bound' to the same exact 1 and will have the same id()
(and therefore, I think memory address). I also get that if you set:
a = [1, 2, 4]
b = a
b[0] = 45
# a is now [45, 2, 4]
because a
and b
are bound to the same list (object), and changes in one result in changes to another. Similarly, a[0]
and b[0]
are the same object. The list contains other objects with different ids - aka list identity is not bound to its contents.
Okay. So far so good. I can accept that there are 'unborn' lists and numbers floating around waiting to initialized (only once though!), and that Python takes care of assigning a memory space for them once we want them. Why then, if I do:
d = [1, 2]
e = [145, 7]
# id(d) and id(e) are not the same?!
Shouldn't there only be a single 2-element list in Python's existence? This would be consistent to me (and then there is only a single 1, a single 2, a single 145...etc).
Any explanation would appreciated - and that includes ones that relate it back to pointers (since I am also somewhat mystified about the decisions that are made at a memory management level, but I suppose that's the concern of Python's execution model and not me!)