From the Python 3.7 changelog:
从Python 3.7更改日志:
the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec.
已经声明dict对象的插入顺序保存性质是Python语言规范的官方部分。
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.
这是否意味着OrderedDict将变得多余?我能想到的唯一用途是保持与旧版本Python的向后兼容性,这些版本不保留普通字典的插入顺序。
1 个解决方案
#1
15
No it won't become redundant because OrderedDict
is not just a dict
that retains insertion order, it also offers order dependent methods like 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不仅仅是一个保留插入顺序的字典,它还提供顺序相关的方法,如OrderedDict.move_to_end(),并支持reverse()迭代。此外,与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
Two relevant questions here and here.
这里和这里有两个相关的问题。
#1
15
No it won't become redundant because OrderedDict
is not just a dict
that retains insertion order, it also offers order dependent methods like 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不仅仅是一个保留插入顺序的字典,它还提供顺序相关的方法,如OrderedDict.move_to_end(),并支持reverse()迭代。此外,与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
Two relevant questions here and here.
这里和这里有两个相关的问题。