Python Mutability Debate
Discussions distinguish between object mutation, variable reassignment, and immutable vs mutable types in Python, including pitfalls like mutable default arguments and operator behaviors like +=.
Activity Over Time
Top Contributors
Keywords
Sample Comments
It's not technically modifying the mutability, it's creating a new mutable x, and setting it to the old x (which is still accessible on the right hand side of the assignment).
The problem with the last code snippet is that it looks like mutating the original value, and that would mislead newcomers to the language.
That's not mutation. Mutation is altering the value of a bound identifier.
I think you're confusing mutation and variable reassignment?
If you were mutating a variable then you'd probably be using the same variable, but in this case you cannot do that. Not trying to be argumentative just saying when you have to store the result in a completely different variable I don't see how that could be considered to look like mutation.
Doesn’t it stop you modifying them inside the function?
There's no mutation there; it's just rebinding the name. Rebinding is very different from mutation. It wouldn't be my stylistic choice either but your FP friend wouldn't be complaining about mutation here.
Which is what every language with a C inheritance does. += has always mutated the original value, I don't know why you expected it to be different.To me the real wtf is the second, where python creates a variable with the same name and discards it.
I was referring to the fact that this function mutates the state of one of its arguments.
a.x isn't a variable, it's a part of an mutable object. JS arrays are also mutable.