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

Franklin? Lee leewangzhong+python at gmail.com
Tue Dec 15 14:18:14 EST 2015


On Tue, Dec 15, 2015 at 1:36 PM, Andrew Barnert <abarnert at yahoo.com> wrote:
> One more point I don't think anyone's brought up yet with the [k: v] syntax: Just as there's no empty set literal because it would be ambiguous with the empty dict, there would be no empty OrderedDict literal because it would be ambiguous with the empty list. The fact that the exact same ambiguity is resolved in opposite directions (which only makes sense if you know the history--dict preceded set in the language, but list preceded OrderedDict) makes it doubly irregular.

I regularly write {} for an empty set and have to fix it later. It
looks like math!

On the other hand, I don't think anyone will learn OrderedDict before
becoming VERY familiar with lists.

If you try to treat a list as an empty OrderedDict, at least you will
fail as soon as you use it.
`empty_list_that_i_think_is_an_ordered_dict[0] = 5` will raise an
error.


> I think Franklin's point was that a list of pairs is _most often_ used as a mapping initializer, but can mean other things as well, some of which might have a need for duplicate keys. For example, a stats package might take a mapping or iterable-of-pairs (the same type the dict constructor takes) for a collection of timestamped data points, and it's perfectly reasonable for two measurements to have the same timestamp in some datasets, but not in others. If the syntax defines an OrderedDict, it can't be used for the first kind of dataset.

No, when I thought about it while writing the email, I was okay with
this syntax NOT having multiple values per key, because I don't think
it's a very basic data structure to think about (there's no builtin
analogous to it), and the indexing syntax wouldn't be like dict's
(indexing gets you a collection of values).

If you really wanted an ordered multidict, you could write `[k1:
[1,2,3], k2: [4,5], k3: [6]]`, or use set displays or tuple displays
instead of list displays. In fact, that just shows how `d = [k1: x,
k2: y, k1: z]` as an OrderedMultiDict would be ambiguous: Is d[k1] a
list, a set, or a tuple? Does k1 come before or after k2?


More information about the Python-ideas mailing list