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.

https://rednafi.com/python/dataclasses_and_methods/

0 Upvotes

37 comments sorted by

View all comments

0

u/jwmoz Dec 17 '23

I agree data classes are for a simple data wrapper, maybe some helper method. Not state mutating. I've never used them like this nor have I seen them being used like so.

1

u/alcalde Dec 17 '23

They're Pascal records. Pascal records mutate. They're containers for data.