[Python-ideas] Dict joining using + and +=

INADA Naoki songofacandy at gmail.com
Mon Mar 4 08:41:38 EST 2019


On Mon, Mar 4, 2019 at 10:29 PM Serhiy Storchaka <storchaka at gmail.com> wrote:
>
> It is not that I like to add an operator for dict merging, but dicts are
> more like sets than sequences: they can not contain duplicated keys and
> the size of the result of merging two dicts can be less than the sum of
> their sizes. Using "|" looks more natural to me than using "+". We
> should look at discussions for using the "|" operator for sets, if the
> alternative of using "+" was considered, I think the same arguments for
> preferring "|" for sets are applicable now for dicts.
>

I concur with Serhiy.  While I don't like adding operator to dict, proposed +/-
looks similar to set |/- than seq +/-.
If we're going to add such set-like operations, operators can be:

* dict & dict_or_set
* dict - dict_or_set
* dict | dict

Especially, dict - set can be more useful than proposed dict - dict.


> But is merging two dicts a common enough problem that needs introducing
> an operator to solve it? I need to merge dicts maybe not more than one
> or two times by year, and I am fine with using the update() method.

+1.

Adding new method to builtin should have a high bar.
Adding new operator to builtin should have a higher bar.
Adding new syntax should have a highest bar.

-- 
INADA Naoki  <songofacandy at gmail.com>


More information about the Python-ideas mailing list