In the Game State section, the author has this sentence:
Normally I avoid dataclasses on account of their being mutable, but here that’s what we need.
I wonder if frozen=True in the dataclass definition is not sufficient for their needs here. Both the data class documentation [1] and the similar documentation [2] for the popular attrs library’s parameter of the same name indicate that true immutability is not possible in Python.
If you want true immutability in your code, Python is not a language to guarantee that.
It’s less that I want “true” immutability and more that I prefer the sorts of code patterns that you’d associate with immutability. In practice I use NamedTuples instead of dataclasses, but as you point out they’re not truly immutable. Usually they’re close enough though.
In the Game State section, the author has this sentence:
I wonder if
frozen=True
in the dataclass definition is not sufficient for their needs here. Both the data class documentation [1] and the similar documentation [2] for the popular attrs library’s parameter of the same name indicate that true immutability is not possible in Python.If you want true immutability in your code, Python is not a language to guarantee that.
[1] https://docs.python.org/3/library/dataclasses.html#frozen-instances
[2] https://www.attrs.org/en/stable/examples.html#immutability
It’s less that I want “true” immutability and more that I prefer the sorts of code patterns that you’d associate with immutability. In practice I use
NamedTuple
s instead ofdataclass
es, but as you point out they’re not truly immutable. Usually they’re close enough though.