3 ms·
The distinction is needed, I think, unless you want to go the purely-functional route. I love that route, but just as many people will complain about purely fun
by Skeime 3y ago
The distinction is needed, I think, unless you want to go the purely-functional route. I love that route, but just as many people will complain about purely functional languages being unintuitive.
If you want mutable values in your language, you need to expect values to mutate. So what should list multiplication do?
- There is the current implementation, where a change to the first entry of [f(x)] * 4 will be reflected in all entries of the list (because they're all references to the same value).
- It could only work on list literals. So [f(x)] * 4 would call f four times and collect the values in a list. Then, a = [f(x)]; a * 4 would not work, which would be surprising.
- It could work differently on list literals and lists. So [f(x)] * 4 would call f four times collecting the results, but a = [f(x)]; a * 4 would just return a list of four references to the one result of f(x).
- It could copy the value in the list. How would you do that if the list contained values like file handlers or network connections that can't be copied?
Within the context of Python (an imperative language with mutable values), the current implementation is the most sensible one. If you work in such a language, you must always exercise some care when working with mutable values. Learn your tool!
Python could have been designed with different decisions. But who knows if we would even be talking about it today in that case?
- kerkeslager 3y ago> If you want mutable values in your language, you need to expect values to mutate. So what should list multiplication do? bitmc can correct me if I'm misinterpreting them, but I feel like what they're saying is that multiplying a list by a number doesn't make sense, so it should throw a type error. That feels reasonable to me (and it's the choice I'm making in the interpreter I'm writing).