I have a python class that contains a list of strings. I want to compare two instances of this class. However, I noticed that if my class gets large the comparison gets quite cumbersome to write. Is there an easier way to write the __eq__ method than this? I use a List instead of a Set because I want to fill this class with data from JSON at a later stage.
json_one = {"var_one": "hello", "var_two": ["foo", "bar"]}
json_two = {"var_one": "hello", "var_two": ["bar", "foo"]}
@dataclass
class Test:
var_one: str
var_two: List[str]
def __eq__(self, other):
if isinstance(other, Test):
return self.var_one == other.var_one\
and sorted(self.var_two) == sorted(other.var_two)
return False
test_one = Test(**json_one)
test_two = Test(**json_two)
if test_one == test_two:
print("yay we're equal with lists in different order")
The fact that you can define an instance of your class using the output of
json.loadshould not dictate your class's design. Rather, define your class "naturally", and provide a method for using a JSON response to create the instance.