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

Glenn Linderman v+python at g.nevcal.com
Wed Jan 30 18:41:15 EST 2019

On 1/30/2019 2:32 PM, Raymond Hettinger wrote:
> Now that regular dicts are ordered and compact, it makes more sense for the _asdict() method to create a regular dict (as it did in its early days) rather than an OrderedDict.
> Option 4) Just make the change directly in 3.8,  s/OrderedDict/dict/, and be done will it.  This gives users the benefits right away and doesn't annoy them with warnings that they likely don't care about.   There is some precedent for this.  To make namedtuple class creation faster, the *verbose* option was dropped without any deprecation period.  It looks like no one missed that feature at all, but they did get the immediate benefit of faster import times.  In the case of using regular dicts in named tuples, people will get immediate and significant space savings as well as a speed benefit.
> 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?
Option 4 sounds good to me.

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).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20190130/711f6a52/attachment-0001.html>

More information about the Python-Dev mailing list