[Python-ideas] PEP: Dict addition and subtraction

Nathan Schneider neatnate at gmail.com
Thu Mar 21 09:32:21 EDT 2019


On Thu, Mar 21, 2019 at 9: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?
>

It depends how abstractly you define the "meanings".

If you define + as "arithmetic addition" and "sequence concatenation", then
yes, there are 2. But novices have to learn that the same concatenation
operator applies to strings as well as lists/tuples. And when reading x +
y, it is probably relevant whether x and y are numbers, strings, or
sequence containers like lists.

The proposal would generalize "sequence concatenation" to something like
"asymmetric sequence/collection combination". (Asymmetric because d1 + d2
may not equal d2 + d1.) It seems a natural extension to me, though the |
alternative is also reasonable (interpreted as taking the OR of keys in the
two dicts; but unlike unioning two sets, the dict-merge operator would be
asymmetric). The third proposed alternative, <<, has no "baggage" from an
existing use as a combination operator, but at the same time it is a more
obscure choice.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190321/af5c3fdc/attachment-0001.html>


More information about the Python-ideas mailing list