[Python-ideas] PEP: Dict addition and subtraction

Antoine Pitrou solipsis at pitrou.net
Fri Mar 15 07:20:21 EDT 2019

On Wed, 6 Mar 2019 00:46:57 +0000
Josh Rosenberg
<shadowranger+pythonideas at gmail.com>
> Overloading + lacks the clear descriptive aspect of update that describes
> the goal of the operation, and contradicts conventions (in Python and
> elsewhere) about how + works (addition or concatenation, and a lot of
> people don't even like it doing the latter, though I'm not that pedantic).
> A couple "rules" from C++ on overloading are "*Whenever the meaning of an
> operator is not obviously clear and undisputed, it should not be
> overloaded.* *Instead, provide a function with a well-chosen name.*"
> and "*Always
> stick to the operator’s well-known semantics".* (Source:
> https://stackoverflow.com/a/4421708/364696 , though the principle is
> restated in many other places).

Agreed with this.  What is so useful exactly in this new dict operator
that it hasn't been implemented, say, 20 years ago?  I rarely find
myself merging dicts and, when I do, calling dict.update() is entirely
acceptable (I think the "{**d}" notation was already a mistake, making
a perfectly readable operation more cryptic simply for the sake of
saving a few keystrokes).

Built-in operations should be added with regard to actual user needs
(such as: a first-class notation for matrix multiplication, making
formulas easier to read and understand), not a mere "hmm this might
sometimes be useful".

Besides, if I have two dicts with e.g. lists as values, I *really*
dislike the fact that the + operator will clobber the values rather than
concatenate them.  It's a recipe for confusion.



More information about the Python-ideas mailing list