[Python-Dev] Guarantee ordered dict literals in v3.7?

Evpok Padding evpok.padding at gmail.com
Tue Nov 7 04:56:53 EST 2017


Hello,

I agree with Wolfgang here. From what I gathered of the discussion, the
argument started from « It would be nice if dict litterals returned ordered
dicts instead of an unordered ones », which is mostly a good thing, as it
allows e.g. `OrderedDict({'spam': 'ham', 'sausages': 'eggs'})` instead of
having to rely on lists of couples to create an OrderedDict. It is not of
utmost utility, but it would be nice to have and not dissimilar to what we
already have with kwargs being ordered dicts, also a matter of slightly
better usability. Possibly, in order to avoid implying that all dicts
guarantee ordering at all time, a syntax such as `o{}` might be used,
mostly to help newcomers. So far, so good.

Where it started to go all haywire is when it became conflated that with
the fact that CPython and Pypy dicts are actually ordered (up to a point at
least) and it suddenly became « Let's guarantee the ordering of all dicts »
which apparently is an issue for at least one implementation of Python, and
still have to be implemented in several others (said implementation would
be trivial, or so it is said, but it still has to be written, along with
appropriate tests, regression checks…). So far, the arguments I have seen
for that are

  1. It is useful in context where one has to guarantee the ordering of
some mapping (such as in json)
  2. It is useful in contexts where ordering is facultative, but nice to
have (debugging has been mentionned)
  3. It is already this way in CPython, so people are going to use that
anyway

I take issue with all of those arguments.

  1. If the ordered should be guaranteed, why would it be so hard to use
OrderedDict ?
    - Just write `d = OrderedDict(((key, val) for key, value in …))`
instead of `{key: value for key, value in …}`. It is not that hard and at
least it is explicit that the order is important. And if it is really so
hard, we could have dict comprehensions be ordered too in addition to
litterals, it still doesn't mean that dicts should all be ordered
    - It is even easier if you fill your dict value-per-value, just
initialise it as `d = OrderedDict` instead of `d = {}` and voilà !
  2. I would like to see some examples of cases where this is really much
more convenient than any other soution, but even then I suspect that these
cases are not sufficently relevant to wed all Python backends to ordered
dicts forever.
  3. This is just a pure fallacy. The language has a documented API that
says that if order of insertion is important, you should explicitely use an
OrderedDict. If people stray away from it and use implementation details
such as the ordering of dict in CPython, they are on their own and
shouldn't expect it to be portable to any other version. Again, it's not as
if OrderedDict did not exist or was much more inconvenient to use than dict.

Also, since the proposed implementation does not keep ordering on deletion,
those relying implicitely on the ordering of dicts without reading the docs
might get bitten by it later in much more subtle ways.
Note that I don't sugest mandatory shuffling of dicts to advertise their
non-guaranteed ordering, either. Just that reading the docs (or having your
instructor tell you that dict does not guarantee the order) is the
reponsibility of the end user.

To sum it up

  - Ordered dict litterals are a nice idea, but probably not that
important. If it happens, it would be nice if it could be extended to dict
comprehensions, though.
  - Guaranteeing the ordering of all `dicts` does not make a lot of sense
  - Changing the API to guarantee the order of dicts **is** an API change,
which still means work

Am I missing something ?

Cheers,

E

On 7 November 2017 at 10:51, Petr Viktorin <encukou at gmail.com> wrote:

> On 11/07/2017 09:00 AM, Wolfgang wrote:
> [...]
>
>> Also it is fine to teach people that dict (Mapping) is not ordered
>> but CPython has an implementation detail and it is ordered.
>> But if you want the guarantee use OrderedDict.
>>
>
> I don't think that is fine.
> When I explained this in 3.5, dicts rearranging themselves seemed quite
> weird to the newcomers.
> This year, I'm not looking forward to saying that dicts behave
> "intuitively", but you shouldn't rely on that, because they're
> theoretically allowed to rearrange themselves.
> The concept of "implementation detail" and language spec vs. multiple
> interpreter implementations isn't easy to explain to someone in a "basic
> coding literacy" course.
>
> Today I can still show an example on Python 3.5. But most people I teach
> today won't run their code on 3.5, or on MicroPython or Brython, and quite
> soon they'll forget that there's no dict ordering guarantee.
>
> Also: I happen to read python-dev and the language docs. I suspect not all
> teachers do, and when they see that dict order randomization was "fixed",
> they might just remove the explanation from the lesson and teach something
> practical instead.
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/evpok.
> padding%40gmail.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171107/d6a462b0/attachment-0001.html>


More information about the Python-Dev mailing list