[Python-ideas] PEP: Dict addition and subtraction

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Fri Mar 8 00:01:20 EST 2019

Ka-Ping Yee writes:
 > On Wed, Mar 6, 2019 at 4:01 PM Chris Angelico <rosuav at gmail.com> wrote:

 > > But adding dictionaries is fundamentally *useful*. It is expressive.
 > It is useful.  It's just that + is the wrong name.

First, let me say that I prefer ?!'s position here, so my bias is made
apparent.  I'm also aware that I have biases so I'm sympathetic to
those who take a different position.

Rather than say it's "wrong", let me instead point out that I think
it's pragmatically troublesome to use "+".  I can think of at least
four interpretations of "d1 + d2"

1.  update
2.  multiset (~= Collections.Counter addition)
3.  addition of functions into the same vector space (actually, a
    semigroup will do ;-), and this is the implementation of
4.  "fiberwise" addition (ie, assembling functions into relations)

and I'm very jet-lagged so I may be missing some.

Since "|" (especially "|=") *is* suitable for "update", I think we
should reserve "+" for some alternative future commutative extension,
of which there are several possible (all of 2, 3, 4 are commutative).

Again in the spirit of full disclosure, of those above, 2 is already
implemented and widely used, so we don't need to use "+" for that.
I've never seen 4 except in the mathematical literature (union of
relations is not the same thing).  3, however, is very common both for
mappings with small domain and sparse representation of mappings with
a default value (possibly computed then cached), and "|" is not
suitable for expressing that sort of addition (I'm willing to say it's
"wrong" :-).

There's also the fact that the operations denoted by "|" and "||" are
often implemented as "short-circuiting", and therefore not
commutative, while "+" usually is (and that's reinforced for
mathematicians who are trained to think of "+" as the operator for
Abelian groups, while "*" is a (possibly) non-commutative operator.  I
know commutativity of "+" has been mentioned before, but the
non-commutativity of "|" -- and so unsuitability for many kinds of
dict combination -- hasn't been emphasized before IIRC.


More information about the Python-ideas mailing list