[Python-ideas] Adding "+" and "+=" operators to dict
Stephen J. Turnbull
stephen at xemacs.org
Sat Feb 14 10:39:39 CET 2015
Donald Stufft writes:
> Or we can choose the interpretation that has already been chosen by
> multiple locations within Python. That keys are replaced and rhs
> wins. This is consistent with basically every location in the stdlib
> and Python core where two dicts get combined in some fashion other than
> specialized subclasses.
Sure, nobody contests that we *can*. To what benefit? My contention
is that even the strongest advocates haven't come up with anything
stronger than "saves typing characters".
True, "dict = dict1 + dict2 + dict3" has a certain amount of visual
appeal vs. the multiline version, but you're not going to go beyond
that and write "dict = (dict1 + dict2)/2", or "dict = dict1 * dict2",
certainly not with "rightmost item wins" semantics. So the real
competition for typing ease and readability is
dict = merged(dict1, dict2, dict3)
and I see no advantage over the function version unless you think that
everything should be written using pseudo-algebraic operators when
possible. And the operator form is inefficient for large dictionaries
(I admit I don't know of any applications where I'd care).
More information about the Python-ideas
mailing list