Will OrderedDict become redundant in Python 3.7?

From the Python 3.7 changelog:

the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec.

Would this mean that OrderedDict will become redundant? The only use I can think of it will be to maintain backwards compatibility with older versions of Python which don't preserve insertion-order for normal dictionaries.

15459 次浏览

不,在 Python 3.7中它不会变得多余,因为 OrderedDict不仅仅是一个保留插入顺序的 dict,它还提供了一个依赖于顺序的方法 OrderedDict.move_to_end(),并且支持 reversed()迭代 * 。

此外,与 OrderedDict的相等性比较是顺序敏感的,而 Python 3.7中的 dict仍然不是这种情况,例如:

>>> OrderedDict([(1,1), (2,2)]) == OrderedDict([(2,2), (1,1)])
False
>>> dict([(1,1), (2,2)]) == dict([(2,2), (1,1)])
True

两个相关问题 给你给你

* 为 Python 3.8添加了对常规 Python dictreversed()迭代的支持,参见 issue33462