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

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


On Thu, Feb 12, 2015 at 09:57:46AM -0700, Carl Meyer wrote:
> On 02/12/2015 03:43 AM, Steven D'Aprano wrote:
> > I think this is a feature that is more useful in theory than in 
> > practice. Which we already have a way to do a merge in place, and a 
> > copy-and-merge seems like it should be useful but I'm struggling to 
> > think of any use-cases for it. I've never needed this, and I've never 
> > seen anyone ask how to do this on the tutor or python-list mailing 
> > lists.
> 
> I think the level of interest in
> http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-in-a-single-expression
> (almost 1000 upvotes on the question alone) does indicate that the
> desire for an expression form of merging dictionaries is not purely
> theoretical.

I'm going to quote Raymond Hettinger, from a recent discussion here:

[quote]
I wouldn't read too much in the point score on the StackOverflow 
question.  First, the question is very old [...] Second, StackOverflow 
tends to award high scores to the simplest questions and answers [...] A 
high score indicates interest but not a need to change the language. A 
much better indicator would be the frequent appearance of ordered sets 
in real-world code or high download statistics from PyPI or
ActiveState's ASPN cookbook.
[end quote]

In this case, we're not talking about ordered sets. We're talking about 
something which is may be as little as two lines of code, and can even 
be squeezed into a single-line function:

def merge(a, b):  d = a.copy(); d.update(b); return d

(but don't do that). StackOverflow's model is designed to encourage 
people to vote on questions as much as possible, and naturally people 
tend to vote more for simple questions that they understand rather than 
hard questions that require a lot of in-depth knowledge or analysis. I 
agree with Raymond that high votes are not in and of themselves evidence 
of frequent need.


-- 
Steve


More information about the Python-ideas mailing list