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

Peter Mawhorter pmawhorter at gmail.com
Thu Feb 12 21:01:58 CET 2015


>
> Honestly I don’t think "put it on PyPI" is a very useful thing in cases
> like
> these. Consuming a dependency from PyPI has a cost, such the same as
> dropping
> support for some version of Python has a cost. For something with an easy
> enough work around the cost is unlikely to be low enough for people to be
> willing to pay it.
>
> Python often gets little improvements that on their own are not major
> enhancements but when looked at cumulatively they add up to a nicer to use
> language. An example would be set literals, set(["a", "b"]) wasn't
> confusing
> nor was it particularly hard to use, however being able to type {"a", "b"}
> is
> nice, slightly easier, and just makes the language jsut a little bit
> better.
>
> Similarly doing:
>
>     new_dict = dict1.copy()
>     new_dict.update(dict2)
>
> Isn't confusing or particularly hard to use, however being able to type
> that
> as new_dict = dict1 + dict2 is more succinct, cleaner, and just a little
> bit
> nicer. It adds another small reason why, taken with the other small
> reasons,
> someone might want to drop an older version of Python for a newer version.
>
> ---
> Donald Stufft
> PGP: 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA
>
>
I'd expect the major constituency that would use an operator for this
(especially over a function) is not well-represented on this list: students
and newer users of python who just want to add two dictionaries together
(they're probably also the ones upvoting that StackOverflow question).
These people aren't going to download anything from PyPI; they probably
don't know that PyPI exists, as I once didn't.

I don't think anyone seriously supports this proposal as a time-saver or
code-elegancer for developers like ourselves: people who are Python experts
and who develop large projects in Python. As demonstrated several times in
this thread, most of us probably already have our own libraries of utility
functions, and adding another one- or three-liner to these is easy for us.
Adding this feature makes a much bigger difference for someone who's
learning to program using Python, or who already knows some programming but
is picking up Python as an additional langauge.

I see both positives and negatives to the proposal from this point of view.
The positive would be ease of use. Python prides itself on being intuitive,
and it really is much more intuitive in its basic everyday operation than
other widespread languages like Java, in large part (IMO) because of its
easy-to-use built-in default data structures. When I was learning Python,
having already learned some C and Java, the idea of built-in list and
dictionary data structures was an enormous relief. No more trying to
remember the various methods of HashMap or ArrayList or worrying about
which one I should be using where: these basic tools of programming just
worked, and worked the way I'd expect, using very simple syntax. That was a
big draw of the language, and I think the argument can be made here that
adding a '+' operator for dictionaries adds to this quality of the language.

{ "a": 1, "b":2 } + { "c": 3}

is the kind of thing that I'd try to see if it worked *before* checking the
docs, and I'd be pleased if it did.

That said, there's a negative here as well. The most obvious update
procedure to me (others can argue about this if they wish) is overwriting
keys from the lhs when a duplicate exists in the rhs. I think a big reason
it seems obvious to me is that this is what I'd do if I had to implement
such a function myself. But whatever resolution you have, it's going to
catch people off-guard and introduce some low-visibility bugs into programs
(especially the programs of the novices who I think are a big target
audience of the idea). Of course, you could go with raising an error, but I
don't think that's very productive, as it forces people to put a try/catch
everywhere they'd use '+' which defeats the purpose of a simpler syntax in
the first place.

Overall I guess I'm +0 on the proposal.
Most useful for novices who just want to add two dictionaries together
already, but also potentially dangerous/frustrating for that same user-base.
A version that raised an exception when there was a key collision would be
pretty useful for novices, but completely useless for experience
programmers, as the overhead of a try/catch greatly outweighs the syntactic
benefit of an operator.

-Peter Mawhorter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150212/711c5da4/attachment.html>


More information about the Python-ideas mailing list