[Python-ideas] adding dictionaries

Alexander Heger python at 2sn.net
Mon Jul 28 22:59:29 CEST 2014


On 29 July 2014 02:08, Guido van Rossum <guido at python.org> wrote:
> In addition, dict(A, **B) is not something you easily stumble upon when your
> goal is "merge two dicts"; nor is it even clear that that's what it is when
> you read it for the first time.
>
> All signs of too-clever hacks in my book.

I try to convince students to learn and *use* python.

If I tell students to merge 2 dictionaries they have to do dict(A,
**B} or {**A, **B} that seem less clear (not something you "stumble
across" as Guidon says) than A + B; then we still have to tell them
the rules of the operation, as usual for any operation.

It does not have to be "+", could be the "union" operator "|" that is
used for sets where
s.update(t)
is the same as
s |= t

... and accordingly

D = A | B | C

Maybe this operator is better as this equivalence is already being
used (for sets).  Accordingly "union(A,B)" could do a merge operation
and return the new dict().

(this then still allows people who want "+" to add the values be made
happy in the long run)

-Alexander


More information about the Python-ideas mailing list