diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-01-31 08:59:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-31 08:59:50 (GMT) |
commit | 0bb4bdf0d93b301407774c4ffd6df54cff947df8 (patch) | |
tree | 174cc59b599a8f9aa223eb0f24c90fccb5e8701f | |
parent | 0897e0c597c065f043e4286d01f16f473ab664ee (diff) | |
download | cpython-0bb4bdf0d93b301407774c4ffd6df54cff947df8.zip cpython-0bb4bdf0d93b301407774c4ffd6df54cff947df8.tar.gz cpython-0bb4bdf0d93b301407774c4ffd6df54cff947df8.tar.bz2 |
bpo-35864: Replace OrderedDict with regular dict in namedtuple() (#11708)
* Change from OrderedDict to a regular dict
* Add blurb
-rw-r--r-- | Doc/library/collections.rst | 9 | ||||
-rw-r--r-- | Doc/whatsnew/3.8.rst | 8 | ||||
-rw-r--r-- | Lib/collections/__init__.py | 4 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2019-01-30-20-22-36.bpo-35864.ig9KnG.rst | 2 |
4 files changed, 21 insertions, 2 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index bfbf8a7..3fa8b32 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -894,11 +894,18 @@ field names, the method and attribute names start with an underscore. >>> p = Point(x=11, y=22) >>> p._asdict() - OrderedDict([('x', 11), ('y', 22)]) + {'x': 11, 'y': 22} .. versionchanged:: 3.1 Returns an :class:`OrderedDict` instead of a regular :class:`dict`. + .. versionchanged:: 3.8 + Returns a regular :class:`dict` instead of an :class:`OrderedDict`. + As of Python 3.7, regular dicts are guaranteed to be ordered. If the + extra features of :class:`OrderedDict` are required, the suggested + remediation is to cast the result to the desired type: + ``OrderedDict(nt._asdict())``. + .. method:: somenamedtuple._replace(**kwargs) Return a new instance of the named tuple replacing specified fields with new diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index fb25ce2..09c43b1 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -125,6 +125,14 @@ New Modules Improved Modules ================ +* The :meth:`_asdict()` method for :func:`collections.namedtuple` now returns + a :class:`dict` instead of a :class:`collections.OrderedDict`. This works because + regular dicts have guaranteed ordering in since Python 3.7. If the extra + features of :class:`OrderedDict` are required, the suggested remediation is + to cast the result to the desired type: ``OrderedDict(nt._asdict())``. + (Contributed by Raymond Hettinger in :issue:`35864`.) + + asyncio ------- diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index c31d7b7..835cc36 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -426,9 +426,11 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non 'Return a nicely formatted representation string' return self.__class__.__name__ + repr_fmt % self + _dict, _zip = dict, zip + def _asdict(self): 'Return a new OrderedDict which maps field names to their values.' - return OrderedDict(zip(self._fields, self)) + return _dict(_zip(self._fields, self)) def __getnewargs__(self): 'Return self as a plain tuple. Used by copy and pickle.' diff --git a/Misc/NEWS.d/next/Library/2019-01-30-20-22-36.bpo-35864.ig9KnG.rst b/Misc/NEWS.d/next/Library/2019-01-30-20-22-36.bpo-35864.ig9KnG.rst new file mode 100644 index 0000000..e3b41b7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-01-30-20-22-36.bpo-35864.ig9KnG.rst @@ -0,0 +1,2 @@ +The _asdict() method for collections.namedtuple now returns a regular dict +instead of an OrderedDict. |