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

Chris Barker chris.barker at noaa.gov
Tue Dec 19 13:04:45 EST 2017


On Tue, Dec 19, 2017 at 8:14 AM, Barry Warsaw <barry at python.org> wrote:

> On Dec 18, 2017, at 22:37, Nathaniel Smith <njs at pobox.com> wrote:
>
> > Wait, what? Why would changing pprint (so that it accurately reflects
> > dict's new underlying semantics!) be a breaking change? Are you
> > suggesting it shouldn't be changed in 3.7?
>
> As others have pointed out, exactly because the current behavior is
> documented.  And we all know that if it’s documented (and often even if
> it’s not, but that’s besides the point here) it will be relied upon.
>

Nathaniel Smith has pointed out that  eval(pprint(a_dict)) is supposed to
return the same dict -- so documented behavior may already be broken.

(though I assume order is still ignored when comparing dicts, so:
eval(pprint(a_dict))
== a_dict will still hold.

But practicality beats purity, and a number of folks have already posted
use-cases where they rely on sorted order, so there you go.


> So we can’t change the default behavior.  But I have no problems
> conceptually with giving users options.  The devil is in the details
> though, e.g. should we special case dictionary sorting only?



> Should we use a sort `key` to mirror sorted() and list.sort()?
>

That would be a nice feature! If anything is done, I think we should allow
a key function.

and maybe have key=None as "unsorted"

-CHB




-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171219/d22b3776/attachment.html>


More information about the Python-Dev mailing list