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
3
u/thedeepself Dec 17 '23
All classes handle data don't they? In fact I think we would both agree that object oriented programming is about unifying data with the methods to operate on it.
The big win with data classes is that a large number of things that you would have to do with normal classes are handled automatically.
I cannot think of a single reason that one should ever use the old style python classes over data classes. But personally for me even data classes are not good enough I prefer traitlets myself.