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

Steven D'Aprano steve at pearwood.info
Sat Feb 14 13:41:03 CET 2015


On Sat, Feb 14, 2015 at 05:32:50PM +1300, Greg Ewing wrote:
> Steven D'Aprano wrote:
> >I think that focusing on the question of "which wins" in the case of 
> >duplicate keys, the left or right operand, is not very productive. We 
> >can just declare that Python semantics are that the last value seen 
> >wins,
> 
> Sure we can, but that's something to be learned. It's
> not going to be obvious to everyone.

*shrug*

You mean people have to learn what programming languages do before they 
can program? Say it isn't so! *wink*

dict.update() goes back to Python 1.5 and perhaps older. That horse has 
long since bolted. Let's just proclaim the argument about *semantics* 
settled, so we can concentrate on the really important part: arguing 
about the colour of the bike-shed. I mean, syntax/interface.

There are various semantics for merging/updating a mapping with 
another mapping. We've covered them repeatedly, I'm not going to repeat 
them here. But the most commonly used one, the most general one, is what 
dict.update does, and even though it is something that needs to be 
learned, it's pretty easy to learn because it matches the left-to-right 
behaviour of other parts of Python.

So let's just agree that we're looking for a not-in-place equivalent to 
dict.update. Anyone who wants one of the less general versions can do 
what Counter does, and subclass.

Do we have consensus that copy-and-update should use the same semantics 
as dict.update?


-- 
Steven


More information about the Python-ideas mailing list