Say, I have two lists:
a = [1,2,3]
and b = [2,3,1]
If I do a a == b
it returns False
,
If I check sorted(a) == sorted(b)
, it returns True
.
Now, I have two objects:
obj1 = {'a': 1, 'b': 2, 'c': [1, 2]}
and obj2 = {'b': 2, 'a': 1, 'c': [1, 2]}
obj1 == obj2
is True, irrespective of the order of keys.
But if obj2 = {'b': 2, 'a': 1, 'c': [2, 1]}
how do I test the equality? Obviously, obj1 == obj2
returns False. sorted(obj1)
will have ['a', 'b', 'c']
, so sorted(obj1) == sorted(obj2)
is kind of waste check.
I should have probably overridden the equality method for the object, or use some library. Is, is there any way to write idiomatic python code for deep equality?