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

Guido van Rossum guido at python.org
Mon Mar 4 14:24:39 EST 2019


* Dicts are not like sets because the ordering operators (<, <=, >, >=) are
not defined on dicts, but they implement subset comparisons for sets. I
think this is another argument pleading against | as the operator to
combine two dicts.

* Regarding how to construct the new set in __add__, I now think this
should be done like this:

class dict:
    <other methods>
    def __add__(self, other):
        <checks that other makes sense, else return NotImplemented>
        new = self.copy()  # A subclass may or may not choose to override
        new.update(other)
        return new

AFAICT this will give the expected result for defaultdict -- it keeps the
default factory from the left operand (i.e., self).

* Regarding how often this is needed, we know that this is proposed and
discussed at length every few years, so I think this will fill a real need.

* Regarding possible anti-patterns that this might encourage, I'm not aware
of problems around list + list, so this seems an unwarranted worry to me.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190304/a7110b64/attachment-0001.html>


More information about the Python-ideas mailing list