News for hackers. Stuff that matters.
Re:Python dicts are now ordered by hprotagonist on Friday February 07, 2020 @11:11AM (#22267352)
If you know you're supporting old code, use OrderedDict.

you arguably ought to anyway, for explicitness.

Re:Python dicts are now ordered by masklinn on Friday February 07, 2020 @11:51AM (#22267887)
OrderedDict is slow and expensive though: it maintains ordering through a doubly linked list.

It has useful features for manipulating ordering but while I've regularly needed had use for maintaining insertion ordering I can't remember ever needing to move items around within a map.

Re:Python dicts are now ordered by hencoappel on Friday February 07, 2020 @05:22PM (#22271746)
If memory serves me correctly, ever since dicts became ordered the OrderedDict simply became a subclass of dict, so it will have exactly the same performance characteristics.
Re:Python dicts are now ordered by masklinn on Friday February 07, 2020 @05:34PM (#22271862)
OrderedDict was always a subclass of dict maintaining additional information (which is not free, it has to store and manipulate two pointers per entry).

It remains so today, ordereddict is not an alias or trivial facade to the normal dict because it has to maintain its doubly linked list and implement a bunch of additional operations based on that e.g. pop first or move to end.

Re:Python dicts are now ordered by edflsafoiewq on Friday February 07, 2020 @05:31PM (#22271838)
Yup.

    >>> isinstance(OrderedDict(), dict)
    True
Re:Python dicts are now ordered by masklinn on Friday February 07, 2020 @05:37PM (#22271888)
That was already the case in python 2.7 or 3.1.
Re:Python dicts are now ordered by harikb on Friday February 07, 2020 @11:17AM (#22267448)
The trouble is I publish a (new) code that advertises itself as working on 3.x and then it turns out it is being used by a person who only had the version prior to this change.

That said, Go made a similar change (from insertion-order to explicitly-randomized) and world didn’t end. So there’s that.

Re:Python dicts are now ordered by ryebit on Friday February 07, 2020 @11:26AM (#22267557)
If your code relies on a minimum python version, you can add `python_requires=">=3.5"` to your setup.py [https://packaging.python.org/guides/distributing-packages-us...] to ensure it's not installed on older releases.

That field itself is kinda new; but if needing to block users with older versions, that shouldn't be an issue.

Re:Python dicts are now ordered by hprotagonist on Friday February 07, 2020 @12:32PM (#22268426)
personally, i just drop a f-string in setup.py and that'll filter out any python for which this issue pertains.
Re:Python dicts are now ordered by baq on Friday February 07, 2020 @01:11PM (#22268882)
This might not work if you’re distributing a wheel.
Re:Python dicts are now ordered by AdamN on Friday February 07, 2020 @12:49PM (#22268625)
Python 3.4 is EOL anyway so there's no need to do this. Anybody running 3.4 is already unsupported.
Re:Python dicts are now ordered by hprotagonist on Friday February 07, 2020 @06:42PM (#22272389)
and 3.5 dies in september. hurray!
Re:Python dicts are now ordered by strken on Friday February 07, 2020 @12:53PM (#22268672)
I thought Go made the change from undefined behaviour with an underlying implementation that was insertion order in a map with 8 or fewer entries, to similarly undefined behaviour with an implementation that randomised lookups in those cases. Any code that has ever relied on any kind of ordering in Go maps will almost certainly be wrong, even random ordering, because the distribution of the "random" ordering is biased.

See https://medium.com/i0exception/map-iteration-in-go-275abb76f...

Re:Python dicts are now ordered by throwaway894345 on Friday February 07, 2020 @12:23PM (#22268335)
FWIW, when Go made that change, it was a much less-widely-used language (smaller blast radius).
Re:Python dicts are now ordered by yurlungur on Friday February 07, 2020 @12:53PM (#22268673)
Yeah I think this is probably my main issue. I don't think it's reasonable to ask users of your code to always use 3.7+ instead of 3.6 if they are usually expected to be compatible. And it's also unnecessary to break such compatibility for something like preferring dict over OrderedDict anyways. At least I would try to avoid any such issues by still using OrderedDict.

That said, I have no idea about the internals of dict. I assume no performance was sacrificed for this change.

Re:Python dicts are now ordered by TheFlyingFish on Friday February 07, 2020 @02:09PM (#22269553)
It actually improves performance. Or at least, it comes along with a set of performance improvements that give you ordering for free. Raymond Hettinger has a great talk on it: https://www.youtube.com/watch?v=npw4s1QTmPg&t=1s
Re:Python dicts are now ordered by viraptor on Friday February 07, 2020 @05:12PM (#22271634)
Using OrderedDict is actually nice in this case, even if the default dict has the same ordering. That way you're explicitly saying you rely on that behaviour and it makes reading the code easier.
Re:Python dicts are now ordered by ouid on Friday February 07, 2020 @11:13AM (#22267397)
right, so making it implicit is bad design
Re:Python dicts are now ordered by mark-r on Friday February 07, 2020 @11:28AM (#22267590)
It's part of the zen of Python: Explicit is better than implicit.
Re:Python dicts are now ordered by skedaddle on Friday February 07, 2020 @01:32PM (#22269099)
Python is nothing like its guiding principles. That's why it's Zen -- the principles are a collection of contradictory statements, given what you will encounter in real world Python. You're meant to be confused and meditate on it.