不可变对象在创建后无法改变;可变对象可以改变。这个区别是 Python 的核心,影响别名、字典键、函数参数和一类细微的 bug。
两个类别
text
Immutable: int, float, bool, str, tuple, frozenset, bytes, None
Mutable: list, dict, set, bytearray, and most custom objects
不可变:"改变"创建一个新对象
python
x = "hello"
x.upper() # returns a NEW string "HELLO" — x is unchanged
x += " world" # rebinds x to a NEW string; the original "hello" is untouched
n =
n +=
