[Python-ideas] PEP: Dict addition and subtraction

Chris Angelico rosuav at gmail.com
Thu Mar 21 09:24:58 EDT 2019


On Fri, Mar 22, 2019 at 12:17 AM Serhiy Storchaka <storchaka at gmail.com> wrote:
>
> 21.03.19 14:51, Chris Angelico пише:
> > ... then, in the interests of productive discussion, could you please
> > explain? What is it about dict addition that makes it harder to
> > understand than other addition?
>
> Currently the + operator has 2 meanings for builtin types (both are
> widely used), after adding it for dicts it will have 3 meanings.
>
> 3 > 2, is not?

I suppose you could call it two (numeric addition and sequence
concatenation), but there are subtleties to the way that lists
concatenate that don't apply to strings (esp since lists are mutable),
so I'd call it at least three already. And what about non-builtin
types? You can add two numpy arrays and it does pairwise addition,
quite different from how lists add together. But in every case, the +
operator means "add these things together". It will be the same with
dicts: you add the dicts together.

Antoine has stated that the problem is NOT understanding what
dict.__add__ does, so I am at a loss as to what the problem IS. We
*already* have many different definitions of "add", according to the
data types involved. That is exactly what polymorphism is for. Why is
it such a bad thing for a dict?

Now, my own opinion is that | would be a better operator than +, but
it's only a weak preference, and I'd be happy with either. Also, to my
understanding, the concerns about "what does addition mean" apply
identically to "what does Or mean", but as we've already seen, my
understanding doesn't currently extend as far as comprehending this
issue. Hence asking.

ChrisA


More information about the Python-ideas mailing list