[Python-ideas] PEP: Dict addition and subtraction

INADA Naoki songofacandy at gmail.com
Mon Mar 4 05:15:14 EST 2019


On Mon, Mar 4, 2019 at 6:52 PM Stefan Behnel <stefan_ml at behnel.de> wrote:
>
> I think the main intentions is to close a gap in the language.
>
>     [1,2,3] + [4,5,6]
>
> works for lists and tuples,
>
>     {1,2,3} | {4,5,6}
>
> works for sets, but joining two dicts isn't simply
>
>     {1:2, 3:4} + {5:6}
>

Operators are syntax borrowed from math.

* Operators are used for concatenate and repeat (Kleene star) in
regular language.
  https://en.wikipedia.org/wiki/Regular_language
  seq + seq and seq * N are very similar to it, although Python used +
instead of
  middle dot (not in ASCII) for concatenate.

* set is directly relating to set in math.  | is well known operator for union.

* In case of merging dict, I don't know obvious background in math or
computer science.

So I feel it's very natural that dict don't have operator for merging.
Isn't "for consistency with other types" a wrong consistency?

> but requires either some obscure syntax or a statement instead of a simple
> expression.
>
> The proposal is to enable the obvious syntax for something that should be
> obvious.

dict.update is obvious already.  Why statement is not enough?

Regards,


More information about the Python-ideas mailing list