[Python-ideas] Adding "+" and "+=" operators to dict
Thomas Kluyver
thomas at kluyver.me.uk
Thu Feb 12 19:21:29 CET 2015
On 12 February 2015 at 09:52, Stephen J. Turnbull <stephen at xemacs.org>
wrote:
> I'm a definite -1 on "+" or "|" for dicts. "+=" or "|=" I can live
> with as alternative spellings for "update", but they're both pretty
> bad, "+" because addition is way too overloaded (even in the shopping
> list context) and I'd probably think it means different things in
> different contexts, and "|" because the wrong operand wins in
> "short-circuit" evaluation.
>
Would this be less controversial if it were a new method rather than an odd
use of an operator? I get the impression that most of the objections are
especially against using an operator.
While I think the operator is elegant, I would be almost as happy with a
method, e.g. updated(). It also makes the semantics very clear when e.g.
Counter objects are involved:
new = a.updated(b)
# equivalent to
new = a.copy()
new.update(b)
It would also be nice to be able to pass multiple dictionaries like
a.updated(b, c), to avoid repeated copying in a.updated(b).updated(c).
Or perhaps even a classmethod:
dict.merged(a, b, c)
Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150212/d86477a8/attachment.html>
More information about the Python-ideas
mailing list