[Python-ideas] Adding "+" and "+=" operators to dict

Steven D'Aprano steve at pearwood.info
Thu Feb 12 19:42:34 CET 2015


On Fri, Feb 13, 2015 at 03:27:15AM +1100, Chris Angelico wrote:
> On Fri, Feb 13, 2015 at 3:21 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> > On Fri, Feb 13, 2015 at 02:46:45AM +1100, Chris Angelico wrote:
> >
> >> Imagine if two people independently build shopping lists
> >
> > Sorry, was that shopping *lists* or shopping *dicts*?
> 
> I've never heard anyone in the real world talk about "shopping dicts"
> or "shopping arrays" or "shopping collections.Sequences". It's always
> "shopping lists". The fact that I might choose to represent one with a
> dict is beside the point. :)

Sounds like you're copying Lua then :-)


> >> - "this is the stuff we need" - and
> >> then combine them.
> >
> > Why would you discard duplicates? If you need 2 loaves of bread, and I
> > need 1 loaf of bread, and the merged shopping list has anything less
> > than 3 loaves of bread, one of us is going to miss out.
> >
> 
> I live in a house with a lot of people. When I was younger, Mum used
> to do most of the shopping, and she'd keep an eye on the fridge and
> usually know what needed to be replenished; but some things she didn't
> monitor, and relied on someone else to check them. If there's some
> overlap in who checks what, we're all going to notice the same need -
> we don't have separate requirements here.

But why is Angelico household shopping list model more appropriate for 
dict addition than the "last value applied wins" model used by 
dict.update? Or the multiset model, where the maximum value wins? Or the 
Counter model where values are added?

One more for luck: the *first* value applied wins, rather than the last. 
You can add new keys that aren't already set, but you cannot change the 
value of existing keys. (You can add new items to Mum's shopping list, 
but if she says 2 loaves of bread, then 2 loaves it is.)


> One person notes that we're
> down to our last few eggs and should buy another dozen; another person
> also notes that we're down to our last few eggs, but thinks we should
> probably get two dozen. Getting *three* dozen is definitely wrong
> here.

Fred is making a quiche and needs a dozen eggs. (Its a big quiche.) 
Wilma is baking some cakes and needs two dozen eggs. If you get less 
than three dozen, someone is going to miss out.

The point I am making is that there are *multiple ways* to "add" two 
dicts, and no one model is best in all circumstances.

Sometimes reading python-ideas is like deja vu all over again. Each time 
this issue gets raised, people seem to be convinced that there is an 
obvious and sensible meaning for dict merging. And they are right. If 
only we could all agree on *which* obvious and sensible meaning.

Which is another way of saying that there is no one obviously correct 
and generally useful way to merge two dicts. Well, actually there is 
one, and we already have the dict.update method to do it. If + 
duplicates that, it's redundant, and if it doesn't, it's non-obvious 
and likely to be of more limited use.

Providing any of these alternate merging behaviours in yur own code is 
very simple, a small helper function of just a few lines will do the 
trick.



-- 
Steve


More information about the Python-ideas mailing list