This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: OrderedDict is comparable to dict
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: benjamin.peterson, cool-RR, rhettinger
Priority: normal Keywords:

Created on 2010-09-10 16:15 by cool-RR, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg116027 - (view) Author: Ram Rachum (cool-RR) * Date: 2010-09-10 16:15
OrderedDict is currently comparable to dict.

I think this is not logical, because a dict doesn't have order, and having an identical order is a necessary condition for a match.

I think that comparing an OrderedDict with a dict makes as much sense as comparing a tuple with a set, and that's currently not allowed. (Always returns False)

Here's a disturbing code snippet executed in Python 3.2a1:

>>> from collections import OrderedDict
>>> d1 = OrderedDict(((1, 2), (3, 4)))
>>> d2 = OrderedDict(((3, 4), (1, 2)))
>>> d1 == d2
False
>>> d1 == {1: 2, 3: 4} == d2
True
msg116028 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-09-10 16:20
This was purposeful. See PEP 372.
msg116029 - (view) Author: Ram Rachum (cool-RR) * Date: 2010-09-10 16:29
Thanks Benjamin. Was there a discussion about this in the mailing list? If so can you give me a link to it?
msg116034 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-10 18:20
That is the documented and tested behavior:  http://docs.python.org/library/collections.html#ordereddict-objects

"""
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.
"""

It was a basic design objective for OrderedDicts to be substitutable for regular dictionaries (that is why it subclasses from dict).  This lets an OD be used just about anywhere in Python where a regular dict is expected.

Also look at:  http://en.wikipedia.org/wiki/Liskov_substitution_principle
History
Date User Action Args
2022-04-11 14:57:06adminsetgithub: 54032
2010-09-10 18:20:45rhettingersetassignee: rhettinger

messages: + msg116034
nosy: + rhettinger
2010-09-10 16:29:44cool-RRsetmessages: + msg116029
2010-09-10 16:20:18benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg116028

resolution: not a bug
2010-09-10 16:15:19cool-RRcreate