16.12.19 04:48, Larry Hastings пише:
As of 3.7, dict objects are guaranteed to maintain insertion order. But set objects make no such guarantee, and AFAIK in practice they don't maintain insertion order either. Should they?
I do have a use case for this. In one project I maintain a "ready" list of jobs; I need to iterate over it, but I also want fast lookup because I soemtimes remove jobs when they're subsequently marked "not ready". The existing set object would work fine here, except that it doesn't maintain insertion order. That means multiple runs of the program with the same inputs may result in running jobs in different orders, and this instability makes debugging more difficult. I've therefore switched from a set to a dict with all keys mapped to None, which provides all the set-like functionality I need.
ISTM that all the reasons why dicts should maintain insertion order also apply to sets, and so I think we should probably do this. Your thoughts?
The current dict implementation is called a "compact dict implementation", because it saves memory in common cases. It was the initial reason of writing it. At the same time there was a need in ordered dicts for kwarg and class namespace. We discovered that slightly modified compact dict implementation preserves order, and that its possible drawback (performance penalty) is too small if exists.
But ordered set implementation is not more compact that the current set implementation (because for dicts the item is 3-word, but for sets it is 2-word). Also the current set implementation has some optimizations that the dict implementation never had, which will be lost in the ordered set implementation.
Take to account that sets are way less used than dicts, and that you can use a dict if you need an ordered set-like object.