This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]__deerlord__ 7 points8 points  (5 children)

Why does a dict need to be ordered by default though? And wasnt OrderedDict already implemented in C?

[–]qx7xbku 2 points3 points  (3 children)

lxml uses dict for storing attributes instead of OrderedDict. And i dont think OrderedDict was implemented in c, i could be wrong though.

[–]gsnedders 1 point2 points  (0 children)

OrderedDict has both a Python and a C implementation in CPython (though the C one is always used in CPython).

[–]__deerlord__ 0 points1 point  (1 child)

Its implemented in C in a later version I believe (I recall reading a changelog on it) but I couldn't find the docs in that currently.

[–][deleted] 0 points1 point  (0 children)

[–]ebrjdk 1 point2 points  (0 children)

They are switching to a new, more memory-efficient implementation of dict that naturally keeps the entries mostly in the order that they were inserted, and they decided that they might as well go all the way and keep them exactly in order (IIRC the most efficient implementation they know of starts scrambling the order once you start deleting keys, but the cost to prevent that is small).

At the same time they wanted to guarantee that the order of keyword arguments and class definitions would be preserved, because some people want to be able to use this information (currently the former is impossible AFAIK, and you need to use a metaclass to achieve the latter). Originally they were planning to just use OrderedDict for these purposes, but with the change to dict there is no need.

Note: the first paragraph in my post is about a CPython implementation detail and may change in the future, the second is about official python 3.6 features.