[Python-ideas] Adding "+" and "+=" operators to dict
Chris Barker
chris.barker at noaa.gov
Wed Feb 18 05:48:33 CET 2015
On Tue, Feb 17, 2015 at 2:22 PM, C Anthony Risinger <anthony at xtfx.me> wrote:
> The premise taken here is that dicts ARE sets that simply happen to have
> values associated with them... hence all operations are against keys ONLY.
> The fact that values tag along is irrelevant.
>
but dicts DO have values, and that's their entire reason for existence --
if the values were irrelevant, you'd use a set....
And because values are important -- there is no such thing as "works the
same as a set". For instance:
# union (first key winning here... IIRC this is how sets actually work)
> >>> d1 | d2
> {'a': 1, 'b': 2, 'c': 3, 'd': 5, 'e': 6}
>
The fact that sets may, under the hood, keep the first key is an
implementation detail -- by definition the first and second duplicate keys
are the same. So there is no guidance here whatsoever as to what to do with
with unioning dicts.
Oh, except .update() provides a precedent that has proven to be useful.
Keeping the second value sure feels more natural to me.
Oh, and I'd still prefer + I don't think most users think of merging two
dicts together as a boolean logical operation....
All of the operations support arbitrary iterables as the RHS! This is NICE.
>
not so sure about that -- again, dicts have values, that's why we use them.
Maybe defaultdict could work this way, though.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150217/3d8eed58/attachment.html>
More information about the Python-ideas
mailing list