On Sat, Jun 27, 2020 at 10:06 PM Rob Cliffe <rob.cli...@btinternet.com> wrote: > > > > On 27/06/2020 05:33, Chris Angelico wrote: > > On Sat, Jun 27, 2020 at 2:29 PM Steven D'Aprano <st...@pearwood.info> wrote: > >> Seriously, I genuinely thought that the existing behaviour was the > >> opposite and that `add` unconditionally added the element. "Last seen > >> wins". If I was designing sets, that's probably how I would design it. > >> After all, it's called *add*, not *add if not already there*. I was so > >> sure that this was the current behaviour that I didn't bother to check > >> it before posting, which is rare for me. > >> > >> So I think this counts as the principle of maximal surprise :-) > > Well, that's not how Python's dicts OR sets work, so that would be > > inconsistent. But feel free to do it your own way in your own > > language. > > > > ChrisA > > _______________________________________________ > > > I don't know what you mean Chris, ISTM that *is* the way dicts work: > > >>> d = { 'one' : 1 } > >>> d['one'] = 1.0 > >>> d > {'one': 1.0} > >>> > >>> d = { 'one' : 1 } > >>> d.update( {'one' : 1.0} ) > >>> d > {'one': 1.0} > > Have I misunderstood you?
Sets behave like dict keys, not their values. Obviously the last *value* will win (because that's how assignment works), but with the keys, the one already in the dictionary will be retained. Switching your example around: >>> d = {1: 'one'} >>> d[1.0] = 'one' >>> d {1: 'one'} >>> d = {1: 'one'} >>> d.update({1.0: 'one'}) >>> d {1: 'one'} >>> dict(((1, 'one'), (1.0, 'one'))) {1: 'one'} >>> {1: 'one', 1.0: 'one'} {1: 'one'} When sets were introduced, I believe this behaviour of dicts was deliberately mimicked for sets, although those who've been Pythonning for longer than I have would be better able to say. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/SZPNUALECZMA4FLF5NV2SJFWJUWD7UID/ Code of Conduct: http://python.org/psf/codeofconduct/