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 +=.

➡️ Stable 0.5x Programming Languages
1,801
Comments
20
Years Active
5
Top Authors
#4081
Topic ID

Activity Over Time

2007
2
2008
19
2009
32
2010
24
2011
49
2012
49
2013
91
2014
134
2015
88
2016
85
2017
98
2018
108
2019
106
2020
132
2021
173
2022
189
2023
131
2024
129
2025
153
2026
9

Keywords

PyLongObject OO e.g foo.bar lang.org bar.baz JS a.x frontend.js i.e mutable mutation variable immutable list object assignment mutate default value

Sample Comments

toast0 • Feb 13, 2023 • View on HN

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).

Ndymium • Apr 17, 2019 • View on HN

The problem with the last code snippet is that it looks like mutating the original value, and that would mislead newcomers to the language.

Locke1689 • Dec 4, 2012 • View on HN

That's not mutation. Mutation is altering the value of a bound identifier.

ramchip • Aug 12, 2025 • View on HN

I think you're confusing mutation and variable reassignment?

nightski • Jul 12, 2022 • View on HN

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.

Google234 • Nov 26, 2020 • View on HN

Doesn’t it stop you modifying them inside the function?

electroly • May 2, 2022 • View on HN

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.

pikzen • Oct 11, 2015 • View on HN

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.

MatthewPhillips • Jul 18, 2013 • View on HN

I was referring to the fact that this function mutates the state of one of its arguments.

djur • Mar 31, 2018 • View on HN

a.x isn't a variable, it's a part of an mutable object. JS arrays are also mutable.