[Python-ideas] PEP: Dict addition and subtraction

Brandt Bucher brandtbucher at gmail.com
Fri Mar 1 19:10:44 EST 2019


While working through my implementation, I've come across a couple of
inconsistencies with the current proposal:

> The merge operator will have the same relationship to the dict.update
method as the list concatenation operator has to list.extend, with dict
difference being defined analogously.

I like this premise. += for lists *behaves* like extend, and += for dicts
*behaves* like update.

However, later in the PEP it says:

> Augmented assignment will just call the update method. This is analogous
to the way list += calls the extend method, which accepts any iterable, not
just lists.

In your Python implementation samples from the PEP, dict subclasses will
behave differently from how list subclasses do. List subclasses, without
overrides, return *list* objects for bare "+" operations (and "+=" won't
call an overridden "extend" method). So a more analogous
pseudo-implementation (if that's what we seek) would look like:

def __add__(self, other):
    if isinstance(other, dict):
        new = dict.copy(self)
        dict.update(new, other)
        return new
    return NotImplemented

 def __radd__(self, other):
    if isinstance(other, dict):
        new = dict.copy(other)
        dict.update(other, self)
        return new
    return NotImplemented

def __iadd__(self, other):
    if isinstance(other, dict):
        dict.update(self, other)
        return self
    return NotImplemented

This is what my C looks like right now. We can choose to update these
semantics to be "nicer" to subclasses, but I don't see any precedent
for it (lists, sets, strings, etc.).

Brandt


On Fri, Mar 1, 2019 at 11:41 AM Brett Cannon <brett at python.org> wrote:

>
>
> On Fri, Mar 1, 2019 at 8:50 AM Brandt Bucher <brandtbucher at gmail.com>
> wrote:
>
>> I’ve never been part of this process before, but I’m interested in
>> learning and helping any way I can.
>>
>
> Thanks!
>
>
>>
>> My addition implementation is attached to the bpo, and I’m working today
>> on bringing it in line with the PEP in its current form (specifically,
>> subtraction operations).
>>
>> https://github.com/python/cpython/pull/12088
>>
>
> When your proposed patch is complete, Brandt, just ask Steven to update
> the PEP to mention that there's a proposed implementation attached to the
> issue tracking the idea.
>
> -Brett
>
>
>>
>>
>> Brandt
>>
>> On Mar 1, 2019, at 08:26, Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> Attached is a draft PEP on adding + and - operators to dict for
>> discussion.
>>
>> This should probably go here:
>>
>> https://github.com/python/peps
>>
>> but due to technical difficulties at my end, I'm very limited in what I
>> can do on Github (at least for now). If there's anyone who would like to
>> co-author and/or help with the process, that will be appreciated.
>>
>>
>> --
>> Steven
>>
>> <dict_addition_pep.txt>
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190301/51d59fc5/attachment.html>


More information about the Python-ideas mailing list