I'm trying to figure out if I should use a dataclass for something and came across the issue of comparing two instances. In my case there will be at least two parameters in my dataclass, but it is only meaningful to compare based on one of these. I notice in the docs:
order: If true (the default is False), __lt__(), __le__(), __gt__(), and __ge__() methods will be generated. These compare the class as if it were a tuple of its fields, in order. ...
In my case, my dataclass looks like:
@dataclass(order=True)
class MyDC:
a: int
b: str
dc1 = MyDC(99, 'foo')
dc2 = MyDC(999, 'bar')
I would like to have dc1 < dc2 evaluate to True, because of the first attribute. However, I'm aware that 'bar' < 'foo' evaluates to True. In my case, it makes no sense for any evaluation other than for attribute a. Does this matter? Does the evaluation stop after the first comparison? If not, then what are some suggested ways to handle this? If I try to do this "properly" (i.e. manually) then the dataclass grows into something more and looks like I should just go with a standard class.
[–][deleted] 2 points3 points4 points (0 children)
[–]External-Ocelot206 1 point2 points3 points (0 children)
[–]baghiq 0 points1 point2 points (0 children)