[Python-ideas] PEP: Dict addition and subtraction

Neil Girdhar mistersheik at gmail.com
Fri Mar 1 12:05:54 EST 2019


Looks like a good start.

I think you should replace all of the lines:
 if isinstance(other, dict):
with
 if isinstance(self, type(other)):


Since if other is an instance of a dict subclass, he should be the one to 
process the addition. On the other hand, if self is an instance of the 
derived type, then we are free to do the combination.


I think you should also change this wording:


"the result type will be the type of the left operand"


since the result type will be negotiated between the operands (even in your 
implemenation).


__sub__ can be implemented more simply as a dict comprehension.


Don't forget to return self in __isub__ and __iadd__ or they won't work.


I think __isub__ would be simpler like this:

def __isub__(self, it):
 if it is self:
  self.clear()
 else:
  for value in it:
 del self[value]
 return self

I don't see why you would bother looking for keys (iter will do that 
anyway).




On Friday, March 1, 2019 at 11:27:54 AM UTC-5, Steven D'Aprano 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 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190301/52248588/attachment.html>


More information about the Python-ideas mailing list