Honestly I would rather withdraw the subtraction operators than reopen the discussion about making dict more like set.
On Mon, Mar 4, 2019 at 12:33 PM Neil Girdhar <mistersh...@gmail.com> wrote: > On Mon, Mar 4, 2019 at 3:22 PM Guido van Rossum <gu...@python.org> wrote: > > > > On Mon, Mar 4, 2019 at 12:12 PM Neil Girdhar <mistersh...@gmail.com> > wrote: > >> > >> On Mon, Mar 4, 2019 at 2:26 PM Guido van Rossum <gu...@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. > > Maybe, but reading through the various replies, it seems that if you > are adding "-" to be analogous to set difference, then the combination > operator should be analogous to set union "|". And it also opens an > opportunity to add set intersection "&". After all, how do you filter > a dictionary to a set of keys? > > >> d = {'some': 5, 'extra': 10, 'things': 55} > >> d &= {'some', 'allowed', 'options'} > >> d > {'some': 5} > > >> > >> > * 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. > > Oh right, good point. Then your way is better since it would preserve > any other data stored by the dict subclass. > > > > -- > > --Guido van Rossum (python.org/~guido) > -- --Guido van Rossum (python.org/~guido)
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/