[Python-Dev] How to update namedtuple asdict() to use dict instead of OrderedDict

Raymond Hettinger raymond.hettinger at gmail.com
Wed Jan 30 23:45:46 EST 2019

> On Jan 30, 2019, at 6:00 PM, David Mertz <mertz at gnosis.cx> wrote:
> Ditto +1 option 4
> On Wed, Jan 30, 2019, 5:56 PM Paul Moore <p.f.moore at gmail.com wrote:
> On Wed, 30 Jan 2019 at 22:35, Raymond Hettinger
> <raymond.hettinger at gmail.com> wrote:
> > My recommendation is Option 4 as being less disruptive and more beneficial than the other options.  In the unlikely event that anyone is currently depending on the reordering methods for the output of _asdict(), the remediation is trivially simple:   nt._asdict() -> OrderedDict(nt.as_dict()).
> >
> > What do you all think?
>> +1 from me on option 4.
>> Paul

Thanks everyone.  I'll move forward with option 4.  In Barry's word, JFDI :-)

> On Jan 30, 2019, at 6:10 PM, Nathaniel Smith <njs at pobox.com> wrote:
> How viable would it be to make OrderedDict smaller, faster, and give
> it a cleaner looking repr?

Not so much.  The implementations substantially different because they have different superpowers.  A regular dict is really good at being a dict while retaining order but it isn't good at reordering operations such as popitem(False), popitem(True), move_to_end(), and whatnot.  An OrderedDict is a heavier weight structure (a hash table augmented by a doubly-linked link) -- it is worse at being a dictionary but really good at intensive reordering operations typical in cache recency tracking and whatnot.  Also, there are long-standing API differences including weak references, ability to assign attributes, an equality operation that requires exact order when compared to another ordered dict etc, as well as the reordering methods.  If it was easy, clean, and desirable, it would have already been done :-)  Overall, I think the OrderedDict is increasingly irrelevant except for use cases requiring cross-version compatibility and for cases that need heavy reordering.  Accordingly, I mostly expect to leave it alone and fall into the not-much-used category like UserDict, UserList, and UserString.

> On Jan 30, 2019, at 3:41 PM, Glenn Linderman <v+python at g.nevcal.com> wrote:

> Would it be practical to add deprecated methods to regular dict for the OrderedDict reordering methods that raise with an error suggesting "To use this method, convert dict to OrderedDict." (or some better wording).

That's an interesting idea.  Regular dicts aren't well suited to the reordering operations (like lists, repeated inserts at the front of the sequence wouldn't be performant relative to OrderedDict which uses double-linked lists internally).  My instinct is to leave regular dicts alone so that they can focus on their primary task (being good a fast lookups).


More information about the Python-Dev mailing list