I was surprised by the picture on p. 20 of Gries et al., which shows a variable (difference
) that was previously set equal first to 26 and to 5 pointing at a new memory location containing 5, while the previous memory location is still there. I would have expected (based on C reflexes) that the same memory location would be overwritten by the new value, but this is not true: instead, Python allocates (semi-persistent) memory for the literal values it sees.
x = 1
print(id(x))
## 10105824
print(id(1))
## 10105824
x = 2
print(id(x))
print(id(1))
## 10105856
## 10105824
Voilà, x
is pointing at a new memory location, 1
is still sitting at the same old memory location.
This is (perhaps better) explained here, with pretty pictures:
Other languages have ‘variables’. Python has ‘names’.