[Python-Dev] Guarantee ordered dict literals in v3.7?
Steven D'Aprano
steve at pearwood.info
Mon Nov 6 23:14:26 EST 2017
On Mon, Nov 06, 2017 at 11:33:10AM -0800, Barry Warsaw wrote:
> If we did make the change, it’s possible we would need a way to
> explicit say that order is not preserved. That seems a little weird
> to me, but I suppose it could be useful.
Useful for what?
Given that we will hypothetically have order-preserving dicts that
perform no worse than unordered dicts, I'm struggling to think of a
reason (apart from performance) why somebody would intentionally use a
non-ordered dict. If performance was an issue, sure, it makes sense to
have a non-ordered dict for when you don't want to pay the cost of
keeping insertion order. But performance seems to be a non-issue.
I can see people wanting a SortedDict which automatically sorts the keys
into some specified order. If I really work at it, I can imagine that
there might even be a use-case for randomizing the key order (like
calling random.shuffle on the keys). But if you are willing to use a
dict with arbitrary order, that means that *you don't care* what order
the keys are in. If you don't care, then insertion order should be no
better or worse than any other implementation-defined arbitrary order.
> I like the idea previously
> brought up that iteration order be deliberately randomized in that
> case, but we’d still need a good way to spell that.
That would only be in the scenario that we decide *not* to guarantee
insertion-order preserving semantics for dicts, in order to prevent
users from relying on an implementation feature that isn't a language
guarantee.
--
Steve
More information about the Python-Dev
mailing list