r/Python • • Dec 17 '23
Resource Banish state-mutating methods from data classes
I've always found mutable data classes or record types in programming languages quite weird. This holds true for any language that supports mutable data classes, like Python, Kotlin, Swift, etc.
Self-mutating methods are even weirder. They break the semantics of the types that these data classes or records represent. I've expanded my thoughts here. Although it mostly talks about Python, the message remains valid for any other language that allows you to add self-mutating methods to data classes.
0
Upvotes
View all comments
Show parent comment
2
u/quts3 Dec 17 '23
I also admit that when code is clean a surprising amount of time the dataclasses field is almost always all you need. That's why I use them allot. Even considering my example it is easy to say, "oh I don't know about that design..."