This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: OrderedDict should not accept dict as parameter
Type: Stage:
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 2.7, Python 2.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: georg.brandl, rhettinger, techtonik
Priority: normal Keywords:

Created on 2013-09-15 16:46 by techtonik, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg197787 - (view) Author: anatoly techtonik (techtonik) Date: 2013-09-15 16:46
http://stackoverflow.com/questions/15733558/python-ordereddict-not-keeping-element-order

I wonder why OrderedDict accepts dict as parameter in a first place? OD is used when order is important and if plain dict is supplied, the order is lost.

    >>> d = {3:4, 1:2}
    >>> OD(d)
    OrderedDict([(1, 2), (3, 4)])

OrderedDict should not accept dict as parameter.
msg197790 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-09-15 16:49
An OrderedDict is a dict subclass and needs to accept the same inputs as dict methods.  This is a guaranteed API and not a bug.

It is not the OrderedDict's fault if you supply an unordered input.  It can't add order after the fact.
msg197795 - (view) Author: anatoly techtonik (techtonik) Date: 2013-09-15 17:20
I don't know if it is bug or feature. There are probably cases when order is not important and OrderedDict is used, but I don't remember any.

Too bad Python doesn't have first class ordered mapping type, so that it could report error if unordered arguments are supplied (such as dict or **kwargs).

tag:wart
msg197798 - (view) Author: anatoly techtonik (techtonik) Date: 2013-09-15 17:23
Is it possible to make strict OrderedDict an optional feature? Like `from features import strict_ordered_dict'?
msg197805 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-09-15 18:25
In general, it is not possible for a hypothetical StrictOrderedDict to know whether its input was ordered or not.  For the specific case of dict, it is possible, but the general case is of course completely general (i.e. if the input has a keys() method, the pairs are loaded with:  for k in other.keys(): od[k] = other[k]).  Those semantics are guaranteed.

Remember, Armin's core concept for OrderedDict was "to remember the order that keys were added, the order is determined by whoever does the adding".

FWIW, the stackoverflow question was resolved trivially.  The learning point is perfectly general (i.e. it explains why you write Decimal('1.1') instead of Decimal(1.1)).
msg197813 - (view) Author: anatoly techtonik (techtonik) Date: 2013-09-15 19:02
On Sun, Sep 15, 2013 at 9:25 PM, Raymond Hettinger
<report@bugs.python.org> wrote:
>
> In general, it is not possible for a hypothetical StrictOrderedDict to know whether its input was ordered or not.

Right. That's why it should not accept input that can only be
unordered (including dict and **kwargs) - this is what I mean by
strict mode.

> Remember, Armin's core concept for OrderedDict was "to remember the order that keys were added, the order is determined by whoever does the adding".

IMHO the statement "the order is determined by whoever does the
adding" is false in 9/10 cases of passed dict. In 9/10 cases whoever
supplies dict or **kwargs argument is unaware of what mistake he is
making, and how many hour she will spend discovering the issue.
msg197972 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-09-17 08:31
> Right. That's why it should not accept input that can only be
> unordered (including dict and **kwargs) - this is what I mean by
> strict mode.

That's not even true: the empty and the one-element dict are always ordered.
History
Date User Action Args
2022-04-11 14:57:51adminsetgithub: 63226
2013-09-17 08:31:39georg.brandlsetnosy: + georg.brandl
messages: + msg197972
2013-09-15 19:02:40techtoniksetmessages: + msg197813
2013-09-15 18:25:49rhettingersetmessages: + msg197805
2013-09-15 17:36:58serhiy.storchakasetstatus: pending -> closed
2013-09-15 17:23:13techtoniksetstatus: closed -> pending

messages: + msg197798
2013-09-15 17:20:13techtoniksetmessages: + msg197795
2013-09-15 16:49:46rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg197790

assignee: rhettinger
resolution: rejected
2013-09-15 16:46:03techtonikcreate