3 ms·
haha, your explanation is exactly the mutable vs immutable thing. dicts are mutable, so, it doesn't create a new object to make an assignment numbers are immu
by aprdm 10y ago
haha, your explanation is exactly the mutable vs immutable thing.
dicts are mutable, so, it doesn't create a new object to make an assignment
numbers are immutable, so, it does.
- kstrauser 10y agoExcept that's 100% wrong here. There's not even a facility for marking an object as immutable in Python, so that wouldn't support user-defined classes at all. Python is always pass-by-object-reference, and assignment is always a pointer operation. It's not special-cased like you're describing.
- e12e 10y agoIndeed, this may or may not be clearer with the help of id() - observe: >>> v=2 >>> id(v) 4148001784 >>> id(2) 4148001784 >>> def m(a): ... a=3 ... >>> m(v) >>> id(3) 4148001800 >>> id(v) 4148001784 >>> v 2 >>> l=[] >>> id(l) 4140973392 >>> def m2(lst): ... lst.append(1) ... >>> m2(l) >>> l [1] >>> id(l) 4140973392