Hacker News new | ask | show | jobs
by b_tterc_p 2737 days ago
Dicts now retain order now (in other words all dicts are ordered dicts). I support this, but I also guarantee this is going to bite me in the ass at least once sharing notebooks with someone on an older version of python as I expect to use this liberally. Ah well.
3 comments

Just to clarify. This behavior is expected since 3.6:

As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python.

As of Python 3.7, this is no longer an implementation detail and instead becomes a language feature.

[1] https://stackoverflow.com/questions/39980323/are-dictionarie...

CPython implementation detail vs. Python language feature remains a relatively meaningless distinction.
Even if you stick purely to CPython, it still matters in that there's no guarantee that it'll still be ordered when you update (if you were reading 3.6 dicts). It's not just an implementation detail for CPython, but an implementation detail for specifically CPython 3.6 (and any other version maintaining that status).

So now that its upgraded to language feature, you can actually expect backwards compatibility in future updates.

CPython is not the only Python interpreter out there.
The documentation sometimes suggests otherwise, but since there is no Python language specification, declaring something "part of the language" seems to have no meaning if it's not "implemented in CPython".

PyPy has had ordered dicts for longer than CPython, and Jython and IronPython don't look like they're ever going to support Python 3, so I'm not sure who this declaration is even even hypothetically relevant to.

It's relevant in the fact that you shouldn't rely on it as a programmer prior to this release. Changing it would have been considered a none breaking change.
Good point.
The other major python implementation, pypy, effectively implements CPython. (And insofar as it doens't, it's heading that way.)
> Dicts now retain order now (in other words all dicts are ordered dicts).

I think you can be more clear here: all dicts are ordered by insertion order (not by key).

Fair. I was referring to the precious data structure for this use case, collections.OrderedDict but that’s not self evident if you’re not familiar.
It's a surprisingly useful feature.
Hi, might you be able to give a short example? Thanks!
Forms (and models) in Django are one such thing. When you define fields on a form at the class level, Django maintains a global counter so the fields can retain their order with respect to each other.

Now, with ordered dictionaries, that order is maintained for free.

Kwarg order in functions is also maintained as far as I know.

One thing I do frequently is create an ordered dict where the keys are either regex patterns or lambda functions that return true or false; and the values are some sort of label (let’s say some sort of document type). Then I’ll create a function that will take that ordered dict plus some data, and return the first label that the data conforms to (e.g. regex pattern matches some text input)

This is a handy way of making classification rules for a many stepped process, where certain rules have priority over others. The alternative would be a lengthy set of ifs and elifs which is both ugly and not fun to edit on the fly.