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

Barry Warsaw barry at python.org
Tue Nov 7 11:50:30 EST 2017


On Nov 7, 2017, at 01:51, Petr Viktorin <encukou at gmail.com> wrote:
> 
> 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.

Perhaps, but IME, it’s not hard to teach someone that in a code review.  Today, if I see someone submit a change that includes an implicit assumption about ordering, I’ll call that out.  I can say “you can’t rely on dicts being ordered, so if that’s what you want, use an OrderedDict or sort your test data”.  That’s usually a localized observation, meaning, I can look at the diff and see that they are assuming dict iteration ordering.

What happens when built-in dict’s implementation behavior becomes a language guarantee?  Now the review is much more difficult because I probably won’t be able to tell just from a diff whether the ordering guarantees are preserved.  Do they delete a key somewhere?  Who knows?   I’m not even sure that would be statically determinable since I’d have to trace the use of that dictionary at run time to see if some “del d[key]” is deleting the key in the dict under review or not.  I can probably only tell that at run time.

So how to I accurately review that code?  Is the order presumption valid or invalid?

Cheers,
-Barry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171107/3b928cbf/attachment-0001.sig>


More information about the Python-Dev mailing list