[Python-ideas] Adding "+" and "+=" operators to dict
Steven D'Aprano
steve at pearwood.info
Fri Feb 13 02:48:05 CET 2015
On Fri, Feb 13, 2015 at 08:30:09AM +1100, Alexander Heger wrote:
> I had started a previous thread on this, asking why there was no
> addition defined for dictionaries, along the line of what Peter argued
> a novice would naively expect.
A programmer is a novice for about 1% of their lifespan as a programmer.
Python should be novice-friendly. It shouldn't be designed around what
novices expect.
I wish that the people blithely declaring that novices expect this and
novices expect that would actually spend some time on the tutor mailing
list. I do spend time on the tutor mailing list, and what I have found
is this:
- novices don't care about dicts much at all
- and updating them even less
- novices rarely try things in the interactive interpreter to see
what they do (one of the things which separates novice from junior
is the confidence to just do it and see what happens)
- on the rare occasion they search for information, they find it very
hard to search for operators (that applies to everyone, I think)
- apart from the mathematical use of operators, which they learn in
school, they don't tend to think of using operators much.
I don't think that "novices would naively expect this" is correct, and
even if it were, I don't think the language should be designed around
what novices naively expect.
> ... some deja-vu on the discussion ...
> yes, the arguments were then as now
>
> 1) whether to use + or |, other operators that suggest asymmetric
> (non-commutative) operation were suggested as well
> - in my opinion dictionaries are not sets, so the use of | to indicate
> set-like behaviour brings no real advantage ... + seems the most
> natural for a novice and is clear enough
I was a novice once, and I was surprised that Python used + for
concatenation. Why would people expect + for something that isn't
addition? I don't know where this idea comes from that it is natural and
obvious, I've had to teach beginners that they can "add" strings or
lists to concatenate them.
As far as I was, and still am, concerned, & is the obvious and most
natural operator for concatenation. [1, 2]+[3, 4] should return [4, 6],
and sum(bunch of lists) should be a meaningless operation, like
sum(bunch of HTTP servers). Or sum(bunch of dicts).
> 2) obviously, there were the same questions whether in case of key
> collisions the elements should be added - i.e., apply the + operator
> to the elements;
No, that's not the argument. The argument is not that "we used the +
operator to merge two dicts, so merging should + the values". The
argument is that in the event of a duplicate key, adding the values
is sometimes a common and useful thing to do.
Take Chris A's shopping list analogy. You need a loaf of bread, so you
put it on your shopping list (actually a dict): {'bread': 1}. I need a
loaf of bread too, so I do the same. Merging your dict and my dict
better have two loaves of bread, otherwise one of us is going to miss
out.
--
Steve
More information about the Python-ideas
mailing list