[Python-ideas] Dict literal use for custom dict classes

Bruce Leban bruce at leban.us
Thu Dec 17 02:03:12 EST 2015


Getting back to the original issue of being able to initialize an
OrderedDict with a literal syntax, one proposal is that all dicts are
ordered. There are actually several intermediate steps between dicts are
unordered and ordered and here are some:

(1) Dict literals preserve order of the original keys. When a literal is
iterated, its keys are returned in declaration order. If the dict is
changed in any way, then no guarantee is made about iteration order.

(2) Dict literals preserve order of the original keys. This order is
preserved even if values are changed as long as no keys are added or
deleted. If a key is added or removed, then no guarantee is made about
iteration order.

(3) Dict literals preserve order of the original keys. This order is
preserved for all keys originally added but new keys may be returned in any
order, even interspersed between original keys. The order of the new keys
is not stable in that it may change anytime the dict is changed in any way.

(4) Dict literals are really ordered dicts.


For the case of initialization, all of the above make OrderedDict{k1: v1,
k2: v2, ...}) work. If that use case is the primary motivator here, then
just implementing option (1) seems sufficient.

--- Bruce
Check out my puzzle book and get it free here:
http://J.mp/ingToConclusionsFree (available on iOS)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151216/273a1e02/attachment.html>


More information about the Python-ideas mailing list