OrderedDict在Python3.7中是否会变得多余?

2024-05-15 01:19:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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.

这是否意味着^{}将变得多余?我能想到的唯一用途是保持与旧版本Python的向后兼容性,后者不保留普通字典的插入顺序。


Tags: ofthetoanobjectsorderchangelogbe
1条回答
网友
1楼 · 发布于 2024-05-15 01:19:04

不,它在Python3.7中不会变得多余,因为OrderedDict不仅仅是一个保留插入顺序的dict,它还提供一个依赖于顺序的方法^{},并支持^{}迭代*。

此外,与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

两个相关问题herehere

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

相关问题 更多 >

    热门问题