[Python-ideas] PEP: Dict addition and subtraction

Stefan Behnel stefan_ml at behnel.de
Wed Mar 6 03:33:54 EST 2019

INADA Naoki schrieb am 05.03.19 um 08:03:> On Tue, Mar 5, 2019 at 12:02 AM
Stefan Behnel wrote:
>> INADA Naoki schrieb am 04.03.19 um 11:15:
>>> Why statement is not enough?
>> I'm not sure I understand why you're asking this, but a statement is 
>> "not enough" because it's a statement and not an expression. It does 
>> not replace the convenience of an expression.
> It seems tautology and say nothing.

That's close to what I thought when I read your question. :)

> What is "convenience of an expression"?

It's the convenience of being able to write an expression that generates
the thing you need, rather than having to split code into statements that
create it step by step before you can use it.

Think of comprehensions versus for-loops. Comprehensions are expressions
that don't add anything to the language that a for-loop cannot achieve.
Still, everyone uses them because they are extremely convenient.

> Is it needed to make Python more readable language?

No, just like comprehensions, it's not "needed". It's just convenient.

> Anyway, If "there is expression" is the main reason for this proposal, 
> symbolic operator is not necessary.

As said, "needed" is not the right word. Being able to use a decorator
closes a gap in the language. Just like list comprehensions fit generator
expressions and vice versa. There is no "need" for being able to write

    [x**2 for x in seq]
    {x**2 for x in seq}

when you can equally well write

    list(x**2 for x in seq)
    set(x**2 for x in seq)

But I certainly wouldn't complain about that redundancy in the language.

> `new = d1.updated(d2)` or `new = dict.merge(d1, d2)` are enough. Python 
> preferred name over symbol in general. Symbols are readable and 
> understandable only when it has good math metaphor.
> Sets has symbol operator because it is well known in set in math, not 
> because set is frequently used.
> In case of dict, there is no simple metaphor in math.

So then, if "list+list" and "tuple+tuple" wasn't available through an
operator, would you also reject the idea of adding it, argueing that we
could use this:

    L = L1.extended(L2)

I honestly do not see the math relation in concatenation via "+".

But, given that "+" and "|" already have the meaning of "merging two
containers into one" in Python, I think it makes sense to allow that also
for dicts.

> It just cryptic and hard to Google.

I honestly doubt that it's something people would have to search for any
more than they have to search for the list "+" operation. My guess is that
it's pretty much what most people would try first when they have the need
to merge two dicts, and only failing that, they would start a web search.

In comparison, very few users would be able to come up with "{**d1, **d2}"
on their own, or even "d1.updated(d2)".

My point is, given the current language, "dict+dict" is a gap that is worth


More information about the Python-ideas mailing list