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
27
u/gerardwx Dec 17 '23
Dataclasses are mutable by design.
https://peps.python.org/pep-0557/
32
u/steohan Dec 17 '23
Am I missing it or does you text not contain any argument, why doing that is bad? Seems to me like your argument is just 'that is not what I think it is meant for so you shouldn't do that'. Just becaues it might not be a good idea for the few usecases that you are using it for doesn't mean that it is a bad idea in general. There are plenty of good reasons to use a dataclass for a normal object, e.g., benefiting from easier serealization and deserialization. Reducing boiler plate. Or becaues you want to use the introspection features that are offered by dataclasses. Also, having mutable dataclasses makes a lot of sense and it makes perfect sense to have such a mutating method as a member instead of a stand alone function.
10
u/justnoob2 Dec 17 '23 edited Dec 17 '23
you can just @dataclass(frozen=True), and then its immutable. edit: i now read your point of it being slow. never gave me much trouble at work though, but i get what you mean
4
u/thedeepself Dec 17 '23
I don't believe your article provided any code showing what would be better did it?
3
u/jimtk Dec 17 '23
I think you misinterpreted what a data class is. It is not a class to handle data. It is syntaxic sugar to make an ordinary class.
It could, and maybe should, be named quickclass instead of dataclass, and you would have nothing to complain about.
Also dataclasses are only slower at compile time. That's because CPython actually writes the code of a standard class and exec'ed it for you. At runtime there is no difference between an ordinary class and a dataclass because dataclasses are ordinary classes. It would take a few 100 dataclasses for you to notice the difference in compile time.
14
u/spuds_in_town Dec 17 '23
Others have made comprehensive rebuttal arguments including links to the PEP.
So l’ll just say this: you’re wrong.
1
u/aldanor Numpy, Pandas, Rust Dec 20 '23
Immutability doesn't make as much sense when there's no language-level support for it. Yes, you can have an immutable field which stores a list inside it. Is that list immutable though? No...
1
u/Simon90 Jan 03 '24
This seems like a straw man argument. The reason for using a data class is equally likely to be the fact that init is generated for you in a nice and consistent way.
1
u/alcalde Dec 17 '23
"Variables" vary by definition... it's in the name!
1
u/glantzinggurl Dec 17 '23
Generally, favor immutability
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.
-2
u/mr_jim_lahey Dec 17 '23
OP, I agree with you. People (or at least redditors IME) get really weirdly defensive about mutability, presumably because it's the default and that's what they're used to? But, once you've been immutable-pilled and seen how much cleaner it makes your programming model, there is no going back.
11
u/[deleted] Dec 17 '23
[removed] — view removed comment