[Python-ideas] PEP: Dict addition and subtraction

Chris Angelico rosuav at gmail.com
Thu Mar 21 08:35:36 EDT 2019


On Thu, Mar 21, 2019 at 10:35 PM Antoine Pitrou <solipsis at pitrou.net> wrote:
> > but it's NOT a new operator, it is making use of an existing one, and sure
> > you could guess at a couple meanings, but the merge one is probably one of
> > the most obvious to guess, and one quick test and you know -- I really
> > can't see it being a ongoing source of confusion.
>
> Did you actually read what I said?  The problem is not to understand
> what dict.__add__ does.  It's to understand what code using the +
> operator does, without knowing upfront whether the inputs are dicts.

The + operator adds two things together. I don't understand the issue here.

You can add integers: 1 + 2 == 3
You can add floats: 0.5 + 1.25 == 1.75
You can add lists: [1,2] + [3,4] == [1,2,3,4]
You can add strings: "a" + "b" == "ab"

And soon you'll be able to add dictionaries. The exact semantics need
to be defined, but it's not fundamentally changing how you interpret
the + operator. I don't understand the panic here - or rather, I don't
understand why it's happening NOW, not back when lists got the ability
to be added (if that wasn't in the very first release).

Conversely, if it's the | operator, it's a matter of merging, and the
same is true. You can merge integers, treating them as bit sets. You
can merge sets. And now you'll be able to merge dictionaries. Same
same.

ChrisA


More information about the Python-ideas mailing list