[Python-Dev] Guarantee ordered dict literals in v3.7? (original) (raw)

Paul Moore p.f.moore at gmail.com
Tue Dec 19 13:47:24 EST 2017


On 19 December 2017 at 17:57, Steve Holden <steve at holdenweb.com> wrote:

On Tue, Dec 19, 2017 at 4:49 PM, Stephen J. Turnbull <turnbull.stephen.fw at u.tsukuba.ac.jp> wrote:

Nathaniel Smith writes: > To make sure I understand, do you actually have a script like this, or > is this hypothetical? I have a couple of doctests that assume that pprint will sort by key, yes. It makes the tests look quite a bit nicer by pprinting the output, and I get sorting (which matters for some older Pythons) for free. (I admit I don't actually use those tests with older Pythons, but the principle stands.) I don't see why we don't do the obvious, namely add the option to use "native" order to the PrettyPrinter class, with the default being backward compatible. Perhaps now key ordering has been pronounced we could either add a "sorted" method to dicts equivalent to the following code. def sorted(self): return {self[k] for k in sorted(self.keys())} Alternatively the sorted built-in could be modified to handle dicts in this way.

I don't think there's any need for this.

Though I still find the assumption of any ordering at all a bit weird I suppose I'll grow used to it.

As far as I'm concerned, dictionaries are still exactly as they were before - key-value mappings with no inherent order. None of my code makes any assumption about the ordering of dictionaries, so it'll be 100% unaffected by this change. I find this whole debate about the "consequences" of mandating insertion order to be completely out of proportion. As far as I'm concerned, the only practical impact is that when you iterate over things like dictionary displays, **kw arguments, etc, you get the "obvious" order, and it's not a lucky accident that you do so.

Certainly, with the order guaranteed, people who currently use OrderedDict will be able to simply use a dict in future - although I'd expect very few people will take advantage of this in the immediate future, as by doing so they'll be restricting their code to Python 3.7+ only, for no significant benefit. And let's not forget that OrderedDict is used far less frequently than plain dictionaries, so we're talking about a small percentage of a tiny percentage of uses of mapping objects in the wild that will in any way be affected by this change.

Paul



More information about the Python-Dev mailing list