[Python-Dev] [issue34221] Any plans to combine collections.OrderedDict with dict

Raymond Hettinger raymond.hettinger at gmail.com
Fri Jul 27 02:35:42 EDT 2018


> On Jul 26, 2018, at 10:23 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> 
> On python-idea,  Miro Hrončok asked today whether we can change the OrderedDict repr from, for instance,
> 
> OrderedDict([('a', '1'), ('b', '2')]) # to
> OrderedDict({'a': '1', 'b': '2'})
> 
> I am not sure what our repr change policy is, as there is a back-compatibility issue but I remember there being changes.

We are allowed to change the repr in future versions of the language.  Doing so does come at a cost though. There is a small performance penalty (see the timings below).  Some doctests will break.  And Python 3.8 printed output in books and blog posts would get shuffled if typed in to Python 3.5 -- this is problematic because one of the few remaining use cases for OrderedDict is to write code that is compatible with older Pythons.  

The proposed repr does look pretty but probably isn't worth the disruption.


Raymond

------------------

$ python3.7 -m timeit -r 7 'from collections import OrderedDict' "OrderedDict([('a', '1'), ('b', '2')])"
200000 loops, best of 7: 1.12 usec per loop
$ python3.7 -m timeit -r 7 'from collections import OrderedDict' "OrderedDict({'a': '1', 'b': '2'})"
200000 loops, best of 7: 1.22 usec per loop
$ python3.7 -m timeit -r 7 'from collections import OrderedDict' "OrderedDict([('a', '1'), ('b', '2')])"
200000 loops, best of 7: 1.13 usec per loop
$ python3.7 -m timeit -r 7 'from collections import OrderedDict' "OrderedDict({'a': '1', 'b': '2'})"
200000 loops, best of 7: 1.2 usec per loop
$ python3.7 -m timeit -r 7 'from collections import OrderedDict' "OrderedDict([('a', '1'), ('b', '2')])"
200000 loops, best of 7: 1.12 usec per loop
$ python3.7 -m timeit -r 7 'from collections import OrderedDict' "OrderedDict({'a': '1', 'b': '2'})"
200000 loops, best of 7: 1.2 usec per loop


More information about the Python-Dev mailing list