Python has shared mutable state. Modifying the con...
# thinking-together
d
Python has shared mutable state. Modifying the contents of one variable can cause another variable to change. For example,
Copy code
>>> a=[1,2,3]
>>> b=a
>>> a[0]=17
>>> b
[17, 2, 3]
I think it is unnatural for the value of
b
to change when I modify
a
. I've seen lots of evidence on the internet that this is a source of confusion for novice programmers who are encountering Python for the first time.
k
This is indeed a gotcha, but for this conversation it's worth separating globals from aliasing. You'd have this problem even if Python had no globals or shared state. (Mutable state is a prerequisite, yes.)
f
I mean, Rust lets you share state as long as it's immutable - globals are just particularly aliasy by default, but local mutable state can be no less shared
d
In Curv, 1. Mutable variables have disjoint state. Assignment has copy semantics. Copy semantics are easier to understand, and they are one of the features I need to avoid having shared mutable state. And this avoids aliasing. 2. Arguments are passed by value, not by reference, and that's another feature that avoids aliasing. 3. When a lambda expression is evaluated, the values of non-local variables are captured in a closure, but they are captured by-value, not by-reference. This feature is closely related to by-value assignment and by-value parameter binding. You could describe this as another situation where aliasing is avoided, I suppose. These 3 features of Curv semantics account for the fact that there is no shared mutable state.
w
@Doug Moen what does "mutable variables have disjoint state" mean? Just "copying" instead of aliasing? (Quotes since copy semantics at its best copies less rather than more.) With 3, I suppose it's why Java (mind I haven't touched Java in years) required closed variables to be final. (Of course, since those variables easily store mutable objects... I never understood why the rigmarole though "rigmarole" seemed like Java's motto.)
d
Curv has no shared mutable state, so variables cannot share mutable state. Modify one mutable variable, and the contents of other variables are guaranteed not to change.