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

Chris Barker chris.barker at noaa.gov
Mon Dec 18 21:11:05 EST 2017

Now that dicts are order-preserving, maybe we should change prettyprint:

In [7]: d = {'one':1, 'two':2, 'three':3}

In [8]: print(d)
{'one': 1, 'two': 2, 'three': 3}

order preserved.

In [9]: pprint.pprint(d)
{'one': 1, 'three': 3, 'two': 2}

order not preserved ( sorted, I presume? )

With arbitrary order, it made sense to sort, so as to always give the same
"pretty" representation. But now that order is "part of" the dict itself,
it seems prettyprint should present the preserved order of the dict.

NOTE: I discovered this making examples for an intro to  Python class -- I
was updating the part where I teach that dicts do not preserve order. I was
using iPython, which, unbeknownst to me, was using pprint under the hood,
so got a different order depending on whether I simply displayed the dict
(which used pprint) or called str() or repr() on it. Pretty confusing.

Will changing pprint be considered a breaking change?



Christopher Barker, Ph.D.

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/20171218/5da004e9/attachment.html>

More information about the Python-Dev mailing list