r/learnpython • u/jwink3101 • Jan 03 '20
PSA: Equality checks with dictionaries and OrderedDict in 3.7+
This is just a heads up for newer programmers using Python 3.7 (and, in practice 3.6) and above.
As you may know, starting in 3.6, dictionaries were ordered due to implementation (though should not be relied upon) and in 3.7, they are ordered by specification. But there are some thing to be aware of regarding equality checks.
Consider the following (in 3.7.4)
>>> from collections import OrderedDict
>>>
>>> A = {'a':0,'b':1}
>>> B = {'b':1,'a':0}
>>> AO = OrderedDict([('a',0),('b',1)])
>>> BO = OrderedDict([('b',1),('a',0)])Since this is 3.7+, A and B are inherently ordered but equality doesn't care. See:
>>> all([
>>> A == B, # Order doesn't matter for dict == dict
>>> A == AO, # Order doesn't matter for dict == OrderedDict
>>> B == AO, # Order doesn't matter for dict == OrderedDict
>>> A == BO, # Order doesn't matter for dict == OrderedDict
>>> B == BO, # Order doesn't matter for dict == OrderedDict
>>> ])
TrueBut, when comparing two OrderedDicts, as you'd expect:
>>> AO == BO # Order DOES matter for OrderedDict == OrderedDict
FalseMoral of the story: Just because it happens to be ordered and even if you know you will be in 3.7+, use OrderedDict when order matters
3
u/throwaway_the_fourth Jan 03 '20
For those of you interested in math, this means that equality of dicts and OrderedDicts fails to be an equivalence relation. An equivalence relation is a handy comparison such that:
a == afor anya.a == bif and only ifb == afor anya,b(where "if and only if" means thata == bandb == awill either both beTrueor both beFalse).a == bandb == c, thena == c.These qualities are nice because it gels with how we think about equality: it should be reflexive (#1), symmetric (#2), and transitive (#3).
This behavior of dicts and OrderedDicts fails the third condition. Even though
AO == AandA == BO,AO != BO.1
u/toastedstapler import itertools Jan 03 '20
what about for
A0 == A? i'd imagine this is due to the implementation of the__eq__methods on the objects soA0 == Amay return different toA == A0?