Raymond Hettinger <[email protected]> added the comment:
> There are no specifications regarding the question.
>From the OrderedDict docs:
"""
Equality tests between OrderedDict objects are order-sensitive and are
implemented as list(od1.items())==list(od2.items()). Equality tests between
OrderedDict objects and other Mapping objects are order-insensitive like
regular dictionaries. This allows OrderedDict objects to be substituted
anywhere a regular dictionary is used.
"""
Also, there are tests to verify these behaviors. From
Lib/test/test_ordered_dict.py:
def test_equality(self):
OrderedDict = self.OrderedDict
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
shuffle(pairs)
od1 = OrderedDict(pairs)
od2 = OrderedDict(pairs)
self.assertEqual(od1, od2) # same order implies equality
pairs = pairs[2:] + pairs[:2]
od2 = OrderedDict(pairs)
self.assertNotEqual(od1, od2) # different order implies inequality
# comparison to regular dict is not order sensitive
self.assertEqual(od1, dict(od2))
self.assertEqual(dict(od2), od1)
# different length implied inequality
self.assertNotEqual(od1, OrderedDict(pairs[:-1]))
> Raising exception is the best thing since it will show
> real bug in applications.
Changing the implementation now will break correct code that relies the
documented behavior. The change would also violate an intentional day one
design goal to have ordered dictionaries be substitutable for regular dicts in
existing code that may not have any concept of order. Per PEP 372:
"""
Is the ordered dict a dict subclass? Why?
Yes. Like defaultdict, an ordered dictionary subclasses dict. Being a dict
subclass make some of the methods faster (like __getitem__ and __len__). More
importantly, being a dict subclass lets ordered dictionaries be usable with
tools like json that insist on having dict inputs by testing isinstance(d, dict)
"""
> I don't agree.
That's not relevant. The time to debate the merits of this API passed 13 years
ago. Guido made the final decision on the equality logic. And now that the
code is deployed and widely adopted, it is far too late to change it.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43691>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com