3 ms·
Isn't it obvious? If the initial value is an empty functional list, set, or map (dict), there's no possibility of it being modified. The problem disappears.
by ScottBurson 10y ago
Isn't it obvious? If the initial value is an empty functional list, set, or map (dict), there's no possibility of it being modified. The problem disappears.
> most Python programmers want their sets to be mutable
They're accustomed to mutable sets because those are all they've known. But ask a Clojure programmer how they feel about functional collections, and you'll be told they're fine. Functional collections are actually easier to use because you don't have to worry about unintended aliasing.
And functional behavior is not strange at all. No programming language I have ever used (and it's a long list) has used shared, mutable semantics for numbers. Consider, in C:
int a = 3;
int b = a;
b++;
After this code sequence, 'a' is still 3, of course! Nobody with programming experience expects 'a' to be incremented at the same time 'b' is; it's not even something you stop to think about -- it's completely automatic. This is true even though I wrote 'b++' rather than 'b = b + 1' -- 'b++' could more easily be misread as a mutating operation on a shared integer object, like 's.add(x)'. However, there's no language I'm aware of in which that's what it means. (You can introduce aliasing intentionally in C++ with a reference, but this requires an explicit '&' in the code.)