3 ms·
This is pretty subtle. I wish more languages explicitly distinguished references and values like C++ and Rust.
by blt 3y ago
This is pretty subtle. I wish more languages explicitly distinguished references and values like C++ and Rust.
- scbrg 3y agoEverything falls in place when you accept that in python all names (including list indexes) are just pointers to an object, and objects aren't copied unless you explicitly copy them. Everything else follows from that. If you had done [1] * 4 you'd ended up with a list of four references to the same object (1) as well. It's just that 1 isn't mutable, so nobody cares.
- deleted 3y ago[deleted]
- bmitc 3y agoF# does this nicely with the mutable keyword and the <- assignment operator.
- Skeime 3y agoIn Python, everything is a reference, so you don't need special syntax to mark them. There just happen to be some types where you cannot change the referenced value in any way. These behave like value types in other languages. (If you believe that they should be marked explicitly, do you also believe the same thing about Rust? I.e. should the Rust compiler throw an error if you never actually change a reference type, as you "should" be using a value type in that case?)