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

Donald Stufft donald at stufft.io
Thu Feb 12 13:25:32 CET 2015


> On Feb 12, 2015, at 5:43 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> 
> On Thu, Feb 12, 2015 at 01:07:52AM -0800, Ian Lee wrote:
>> Alright, I've tried to gather up all of the feedback and organize it in
>> something approaching the alpha draft of a PEP might look like::
>> 
>> 
>> Proposed New Methods on dict
>> ============================
>> 
>> Adds two dicts together, returning a new object with the type of the left
>> hand operand. This would be roughly equivalent to calling:
>> 
>>>>> new_dict = old_dict.copy();
>>>>> new_dict.update(other_dict)
> 
> A very strong -1 on the proposal. We already have a perfectly good way 
> to spell dict += , namely dict.update. As for dict + on its own, we have 
> a way to spell that too: exactly as you write above.
> 
> 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’ve wanted this several times, explicitly the copying variant of it. I always
get slightly annoyed whenever I have to manually spell out the copy and the
update.

I still think it should use | rather than + though, to match sets.

---
Donald Stufft
PGP: 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA



More information about the Python-ideas mailing list