On Mon, Mar 4, 2019 at 2:26 PM Guido van Rossum <guido@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.
That ship has long sailed.
> * 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.
No, the values won't be copied -- it is a shallow copy that only increfs the keys and values.
--