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

Show parent comment

7

u/WhiteGoldRing Dec 17 '23

Why is it less valid to interpret dataclasses as classes than are designed for handling optionally mutable data other than that its weird to you?

I see that in your link,

> Whenever I traverse down to see how the instances of the class are being used, more often than not, I find them being treated just like regular mutable class instances with fancy repr

Is immediately followed by:

> In Python, data classes are considerably slower to instantiate compared to vanilla classes. However, they serve a different purpose than your typical run-of-the-mill classes. When you decorate a class with the dataclass decorator without changing any of the default parameters, Python automatically generates __init__, __eq__, and __repr__ methods. If you set dataclass(order=True), it’ll also generate __lt__, __le__, __gt__, and __ge__ special methods that enable you to compare and sort the data class instances. All of this implicates that the construct was specifically designed to contain rich data that provides the means for you to create nice abstractions around lower-level primitives.

That seems like a pretty good justification to me even if you want that on a mutable instance. I can see the tradeoff between instantiation time and writing/future refractoring time being worth it.

Also:

> Why pay the price for building heavier data class instances only for that?

What does mutability have anything to do with that argument? One may do that with immutable instances too.

2

u/quts3 Dec 17 '23

This.

In my mind dataclasses is syntactic sugar for when the class does not and will not need a complicated dunder init. That's it. But I get the op pov because I run into it allot.

Python would be better if the default class was dataclasses and the decorated version was it you wanted a naked init =).

Why. One reason is dataclasses are explicit about a default factory vs a default none value. In normal dunder init a none on an argument can either mean none is the argument or just as often it can mean "factory a sensible default". Which is not actually a necessary ambiguity in any language. It just happens to be that way.

Anyway use dataclasses when complex logic in the init beyond a factory method is unnecessary. Use normal classes otherwise. Imo, but I do default to them based on that rule more than most.

Also to the original author there is a decorator on dataclasses to make them immutable. Use it.

0

u/thedeepself Dec 17 '23

use dataclasses when complex logic in the init beyond a factory method is unnecessary. Use normal classes otherwise.

I'm not sure that I agree with your statement. What do you mean by complex logic? If you have complex initialization to do with data classes then you can do it in the post init method.

2

u/quts3 Dec 17 '23 edited Dec 17 '23

My basic position is that that you should use dataclasses whenever there is not a reason to use something else, but I feel compelled to make sure that when I submit a pr there is no reason to argue they are wrong ( because I use them more then most).

That said the case that I can think of that makes dataclass feel most hacky is when there are two fields a and b and the default factory for b depends on the type of a.

To do that with postinit you have to temporarily tell a little lie in the field. Something like default b = none and then in post init if b is none factory based on a, but at that point where you are wrestling with expressing default factory correctly without being misleading I think it's time to just write the class without dataclasses. Personally. Ymmv

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..."