[docs] [issue24721] The result of calling dict.* methods on OrderedDict is undefined.

Eric Snow report at bugs.python.org
Sat Jul 25 19:10:58 CEST 2015


New submission from Eric Snow:

(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.

----------
assignee: docs at python
components: Documentation
messages: 247354
nosy: docs at python, eric.snow, rhettinger
priority: low
severity: normal
status: open
title: The result of calling dict.* methods on OrderedDict is undefined.
type: behavior
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24721>
_______________________________________


More information about the docs mailing list