[Python-Dev] Guarantee ordered dict literals in v3.7?

Steve Holden steve at holdenweb.com
Tue Dec 19 12:57:17 EST 2017


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. Though I still find the assumption of any ordering at all a bit weird
I suppose I'll grow used to it.

regards
 Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171219/17e6cc2a/attachment.html>


More information about the Python-Dev mailing list