6 ms·
Does this mean all dicts are ordered in Python 3.6 onwards? If so I can see scope for subtle bugs as code written and tested on Python 3.6 will potentially fai
by andybak 10y ago
Does this mean all dicts are ordered in Python 3.6 onwards?
If so I can see scope for subtle bugs as code written and tested on Python 3.6 will potentially fail on earlier versions due to dict order.
But I guess that's why you test using tox...
- collyw 10y agoI am trying to imagine a bug that would happen with an ordered dictionary which would not happen on an unordered dictionary. An unordered dictionary may return it ordered by chance.
- wiz21c 10y agochance my friend is what hides many bugs
- deleted 10y ago[deleted]
- coldtea 10y agoParent said for bugs happening in the other direction -- written for 3.6, run on earlier versions, so it would be the inverse happening (ordered assumption not holding).
- njharman 10y agoReally? It's in the freaking name. Code is written that depends on dictionary being ordered. Works in CPython 3.6, fails on earlier CPython and other implementations of Python.
- collyw 10y agoSorry I wrote that back to front. Fixed now.
- brudgers 10y agoI suspect that any procedure relying on dictionary order can experience all the classes of bugs associated with relying on mutated state. For example, if procedure P1 relies on dictionary order and calls another procedure P2 on a dictionary, might get a new dictionary back in a case where P2 is written in a functional rather than imperative style. Essentially, relying on ordering requires tracking identities rather than values. Which suggests a second bug. What does it mean now mean for two dictionaries to be equal?
- sametmax 10y agoIf you delete a key, and add one, the order can be lost.
- eva1984 10y agoBut it is correct in the sense that the insertion order has already changed.
- OskarS 10y agoThat's an argument against ever adding ANYTHING to a new language! "Oh, we have this neat feature in mind, but because code that uses it wont work on older versions of the compiler/interpreter, we can't use it!" Python code that will depend on the ordering will either have to specify Python 3.6 as the minimum version, or just use OrderedDict, which is presumably isn't going anywhere. Also, it's rare that anything "depends" on the ordering of dictionary keys, but it's frequently "nice to have". For instance, it means that if you output JSON, all the keys are in a sensible order. This matters not at all for other computers reading the JSON (as the JSON spec explicitly says that ordering of keys is irrelevant), but it's lovely for developers debugging the program.
- jamiesonbecker 10y ago> That's an argument against ever adding ANYTHING to a new language! Not exactly, because no error will be raised from Python and subtle bugs could be introduced if someone relied on this behavior.