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

Neil Girdhar mistersheik at gmail.com
Mon Mar 4 15:07:28 EST 2019


On Mon, Mar 4, 2019 at 2:26 PM Guido van Rossum <guido at python.org> wrote:
>
> * 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.
>

I feel like dict should be treated like sets with the |, &, and -
operators since in mathematics a mapping is sometimes represented as a
set of pairs with unique first elements.  Therefore, I think the set
metaphor is stronger.

> * 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

I like that, but it would be inefficient to do that for __sub__ since
it would create elements that it might later delete.

def __sub__(self, other):
 new = self.copy()
 for k in other:
  del new[k]
return new

is less efficient than

def __sub__(self, other):
 return type(self)({k: v for k, v in self.items() if k not in other})

when copying v is expensive.  Also, users would probably not expect
values that don't end up being returned to be copied.

>
> 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.
>

I agree with these points.

Best,

Neil
> --
> --Guido van Rossum (python.org/~guido)
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/zfHYRHMIAdM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> _______________________________________________
> 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/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/zfHYRHMIAdM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


More information about the Python-ideas mailing list