Check dict implementation details
Since dict is ordered in CPython 3.6, it can be used instead of OrderedDict in some places (e.g. for implementing simple limited caches). But since this is implementation detail, it can't be used in the stdlib unconditionally. Needed a way to check whether dict is ordered. Actually there are two levels of "ordering". 1. Dict without deletions is iterated in the order of adding items. Raymond's original compact dict implementation satisfied this claim. 2. In addition the order is preserved after deletion operations. Naoki's implementation satisfies this more strong claim.
On Oct 8, 2016, at 3:01 AM, Serhiy Storchaka
wrote: Since dict is ordered in CPython 3.6, it can be used instead of OrderedDict in some places (e.g. for implementing simple limited caches). But since this is implementation detail, it can't be used in the stdlib unconditionally. Needed a way to check whether dict is ordered.
I think this is premature and shouldn't be done. Instead, everyone (including us) should follow Guido's directive that for now, the right way to provide an ordered dictionary is to use collections.OrderedDict. In the future, we may reimplement collections.OrderedDict() in terms of the new compact dict and everything that uses OrderedDict will benefit from the change. But for now, I think you should avoid wholesale rewrites of everything in the standard library that uses OrderedDict. It seems to me that "needed a way to check whether dict is ordered" is just an end-run around Guido's decision. Also, from a maintenance standpoint, we don't really want two code paths. It is better to just let the new dict implementation play itself out. There is no reason to rush to change lots of code that is currently working just fine. Raymond
On 8 October 2016 at 20:01, Serhiy Storchaka
Since dict is ordered in CPython 3.6, it can be used instead of OrderedDict in some places (e.g. for implementing simple limited caches). But since this is implementation detail, it can't be used in the stdlib unconditionally. Needed a way to check whether dict is ordered.
As Raymond suggests, if order actually matters for a given use case, then use collections.OrderedDict unconditionally without worrying about the behaviour of the default dict implementation. In addition to reducing code churn and improving cross-version and cross-implementation compatibility, doing that also lets the *reader* of the code know that the key iteration order matters. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
On Sat, Oct 8, 2016 at 6:01 AM, Serhiy Storchaka
Since dict is ordered in CPython 3.6, it can be used instead of OrderedDict in some places (e.g. for implementing simple limited caches). But since this is implementation detail, it can't be used in the stdlib unconditionally. Needed a way to check whether dict is ordered.
Actually there are two levels of "ordering".
1. Dict without deletions is iterated in the order of adding items. Raymond's original compact dict implementation satisfied this claim.
2. In addition the order is preserved after deletion operations. Naoki's implementation satisfies this more strong claim.
Sidenote: OrderedDict, unlike dict, is a sequential container (though not a Sequence), so order matters when doing comparisons, and OrderedDicts can be reverse-iterated. That might keep dict from replacing OrderedDict in some cases. Something to keep in mind if this topic is revisited.
participants (4)
-
Franklin? Lee
-
Nick Coghlan
-
Raymond Hettinger
-
Serhiy Storchaka