Message247354
(see issue24667)
collections.OrderedDict subclasses dict so calling dict's methods on an OrderedDict works. However, neither the pure Python nor the C implementation of OrderedDict was written to support doing so. In fact, both of them currently enter an inconsistent state when this happens. For example:
# Python 3.4 (pure Python implementation)
>>> from collections import OrderedDict
>>> od = OrderedDict([('spam', 1), ('eggs', 2)])
>>> dict.__delitem__(od, 'spam')
>>> str(od)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/reprlib.py", line 24, in wrapper
result = user_function(self)
File "/usr/lib/python3.4/collections/__init__.py", line 198, in __repr__
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
File "/usr/lib/python3.4/_collections_abc.py", line 485, in __iter__
yield (key, self._mapping[key])
KeyError: 'spam'
# Python 3.5 (C implementation)
>>> from collections import OrderedDict
>>> od = OrderedDict([('spam', 1), ('eggs', 2)])
>>> dict.__delitem__(od, 'spam')
>>> str(od)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'spam'
This is a consequence of subclassing a builtin type, which typically do not have good support for subclassing (e.g. issue10977).
It probably isn't worth making any changes to the code of either OrderedDict implementations. At most I'd recommend a note in the OrderedDict documentation indicating that the results of passing an OrderedDict object to dict.* methods are undefined. |
|
Date |
User |
Action |
Args |
2015-07-25 17:10:58 | eric.snow | set | recipients:
+ eric.snow, rhettinger, docs@python |
2015-07-25 17:10:58 | eric.snow | set | messageid: <1437844258.42.0.543403557619.issue24721@psf.upfronthosting.co.za> |
2015-07-25 17:10:58 | eric.snow | link | issue24721 messages |
2015-07-25 17:10:57 | eric.snow | create | |
|