3 ms·
> int, float Immutable? Could you explain this a bit more? (As far as I know, "x += 1" will modify the block of memory that x points to)
by dbaupp 15y ago
> int, float
Immutable? Could you explain this a bit more? (As far as I know, "x += 1" will modify the block of memory that x points to)
- DanWaterworth 15y agoNo, I had to check that, but it doesn't appear to be true. `id` uses the ptr of the object in CPython, and: >>> a = 1 >>> id(a) 32855992 >>> a += 1 >>> id(a) 32855968 So it's actually creating a new Integer object, which `a` then points to, or in other words: a += 1 is equivalent to a = a + 1 This behavior makes sense, if you wrote: a = 10 b = a a += 20 You wouldn't expect b to be 30.