Re:Attrs – the Python library everyone needs (2016)
by ericvsmith on Saturday December 25, 2021 @05:58PM
(#29687825)
dataclasses makes it easy to just add your own `__eq__` method, if the default doesn't suit you. You just define it! Nothing else is required.
So I wouldn't be so quick to abandon dataclasses in such a case.
Re:Attrs – the Python library everyone needs (2016)
by masklinn on Monday December 27, 2021 @02:20PM
(#29705812)
> dataclasses makes it easy to just add your own `__eq__` method, if the default doesn't suit you. You just define it! Nothing else is required.
That's completely besides the point, and lots of non-data objects should not be equatable at all, if they even can technically be.
Re:Attrs – the Python library everyone needs (2016)
by ericvsmith on Monday December 27, 2021 @03:28PM
(#29706431)
I don't understand your point. Are you saying that `__eq__` shouldn't be implemented at all? There's a switch for that, too.
Re:Attrs – the Python library everyone needs (2016)
by masklinn on Tuesday December 28, 2021 @07:12AM
(#29712997)
I was merely noting that dataclasses are mostly intended for data holder objects (hence data classes), and thus defaulting to structural equality makes perfect sense, even ignoring it being overridable or disableable.
This was in reply to this objection:
> eq is a property of the domain itself; two objects are only equal if it makes sense in the domain logic for them to be equal, and in many cases that equality is more or less complicated than attribute equality.
It makes perfect sense that attributes be implementation details by default, and `@dataclass` is one of the ways to say they're not.
> eq is a property of the domain itself; two objects are only equal if it makes sense in the domain logic for them to be equal, and in many cases that equality is more or less complicated than attribute equality.
dataclass is intended for data holders, for which structural equality is an excellent default,
If you need a more bespoke business objects, then you probably should not use a dataclass.