[Python-ideas] Adding "+" and "+=" operators to dict
Alexander Heger
python at 2sn.net
Sat Feb 14 19:25:37 CET 2015
>> However, this thread started with a desire for the + and += operators
>> -- essentially syntactic sugar ( which I happen to like ). So is there
>> a real desire / use case for easy to spell merge-into-new-dict
>> behavior at all?
>
> I think there is, for the same reason that there is a use-case for
> sorted() and reversed(). (Only weaker, otherwise we'd already have it.)
> Sometimes you want to update in place, and sometimes you want to make a
> copy and update. So there's definitely a use-case.
I think the issue is not that there is a weaker use case - it's as
good, if not stronger - but the discussion has been more emotional as
it started off with "adding" dictionaries using some operator(s), +=
and +; and whereas I still like the operator syntax as shorthand,
having a "merged" function and maybe extension of the "update" method
for multiple dicts, or an "updated" function would definitively a good
start - and the question on operator or not as shorthand for these can
be fought later and separately. For me, having a "merged" function
would definitely make a lot of code a lot more legible, as Peter
suggested. I merge/update dictionaries a lot more often than I sort
things, it probably is somewhere between 1 in 10 to 1 in 100.
> I'm still not *really* happy about using an operator for this -- it
> doesn't feel like "operator territory" to me -- but if it had to be an
> operator, | is the right one.
There is no "right" one. See previous post. You say it is "more
like" - but it's not the same, either way, for "+" or for "|" on sets.
Maybe we do want people to understand it is not the same as set
combination?
-Alexander
More information about the Python-ideas
mailing list