OrderedDict 在 Python 3.7 中会变得多余吗? [英] Will OrderedDict become redundant in Python 3.7?
问题描述
来自 Python 3.7 变更日志:
<块引用>dict 对象的插入顺序保留特性已声明成为 Python 语言规范的官方部分.
这是否意味着 OrderedDict
会变得多余吗?我能想到的唯一用途是保持与旧版 Python 的向后兼容性,旧版 Python 不保留普通词典的插入顺序.
不,它不会在 Python 3.7 中变得多余,因为 OrderedDict
不仅仅是一个保留的 dict
插入顺序,它还提供了一个依赖顺序的方法,OrderedDict.move_to_end()
,并支持reversed()
迭代*.
此外,与 OrderedDict
的相等比较是顺序敏感的,而 Python 3.7 中的 dict
仍然不是这种情况,例如:
* 为 Python 3.8 添加了对常规 Python dict
的 reversed()
迭代的支持,参见 issue33462
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.
No it won't become redundant in Python 3.7 because OrderedDict
is not just a dict
that retains insertion order, it also offers an order dependent method, OrderedDict.move_to_end()
, and supports reversed()
iteration*.
Moreover, equality comparisons with OrderedDict
are order sensitive and this is still not the case for dict
in Python 3.7, for example:
>>> OrderedDict([(1,1), (2,2)]) == OrderedDict([(2,2), (1,1)])
False
>>> dict([(1,1), (2,2)]) == dict([(2,2), (1,1)])
True
Two relevant questions here and here.
* Support for reversed()
iteration of regular Python dict
is added for Python 3.8, see issue33462
这篇关于OrderedDict 在 Python 3.7 中会变得多余吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!