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

Steven D'Aprano steve at pearwood.info
Mon Dec 18 22:41:55 EST 2017


On Mon, Dec 18, 2017 at 06:11:05PM -0800, Chris Barker wrote:

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? )

Indeed. pprint.PrettyPrinter has separate methods for OrderedDict and regular dicts, and the method for printing dicts calls sorted() while the other does not.

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.

I disagree. Many uses of dicts are still conceptually unordered, even if the dict now preserves insertion order. For those use-cases, insertion order is of no interest whatsoever, and sorting is still "prettier".

-- Steve



More information about the Python-Dev mailing list