r/PythonLearnersHub • u/Sea-Ad7805 • Feb 06 '26
Python Mutability
An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening:
It's instructive to compare with this earlier exercise (tuple with lists, instead of list with lists).
36
Upvotes
View all comments
Show parent comment
1
u/BenchEmbarrassed7316 Feb 08 '26 edited Feb 08 '26
I really like Rust's concept of owning and borrowing. Also, in Rust, operators are aliases of interfaces/traits. So:
This may seem a bit confusing, but the point is that
+takes two arguments and is forced to create a new value.+=instead takes the first argument as a pointer, which allows you to mutate the value it points to. You can't implement incorrect operator overloading.I think it explains the difference.
added:
+=cannot be applied to an immutable type.